class CustomSysmodel : public QFileSystemModel { Q_OBJECT public: CustomSysmodel(QWidget parent = Q_NULLPTR) : QFileSystemModel(parent), m_limit(100), m_timer(new QTimer(this)), m_threadPool(new QThreadPool(this)) { connect(m_timer, &QTimer::timeout, this, &CustomSysmodel::loadMoreFiles); m_timer->setInterval(2000); } public slots: void fetchFiles(const QString &path) { emit started(); FileSystemWorker worker = new FileSystemWorker; connect(worker, &FileSystemWorker::fileFound, this, &CustomSysmodel::fileFound); connect(worker, &FileSystemWorker::finished, this, &CustomSysmodel::finished); worker->setPath(path); m_threadPool->start(worker); } private: int m_limit; QTimer *m_timer; QThreadPool *m_threadPool; }; class FileSystemWorker : public QObject, public QRunnable { Q_OBJECT public: FileSystemWorker(QObject *parent = nullptr) : QObject(parent) {} void setPath(const QString& path) { m_path = path; } void run() override { QFileInfoList files = QDir(m_path).entryInfoList(QDir::Files); foreach (const QFileInfo &fileInfo, files) { emit fileFound(fileInfo.absoluteFilePath()); } emit finished(); } signals: void started(); void finished(); void fileFound(const QString &filePath); private: QString m_path; }; 将这段代码改成多线程读取,给出完整的修改方案
时间: 2024-02-21 09:57:39 浏览: 111
qt_model.rar_QT_Qt model vlew
以下是将该代码改成多线程读取的完整修改方案:
```cpp
class CustomSysmodel : public QFileSystemModel
{
Q_OBJECT
public:
CustomSysmodel(QWidget *parent = nullptr)
: QFileSystemModel(parent)
, m_limit(100)
, m_timer(new QTimer(this))
, m_threadPool(new QThreadPool(this))
{
connect(m_timer, &QTimer::timeout, this, &CustomSysmodel::loadMoreFiles);
m_timer->setInterval(2000);
}
public slots:
void fetchFiles(const QString &path)
{
emit started();
FileSystemWorker *worker = new FileSystemWorker(path);
connect(worker, &FileSystemWorker::fileFound, this, &CustomSysmodel::fileFound);
connect(worker, &FileSystemWorker::finished, this, &CustomSysmodel::finished);
m_threadPool->start(worker);
}
private slots:
void loadMoreFiles()
{
int rowCount = this->rowCount();
int diff = m_filePaths.size() - rowCount;
if (diff > 0) {
beginInsertRows(QModelIndex(), rowCount, rowCount + diff - 1);
for (int i = rowCount; i < rowCount + diff && i < m_limit; i++) {
QFileInfo fileInfo(m_filePaths.at(i));
QFileSystemModel::fetchMore(QFileSystemModel::index(fileInfo.path()));
}
endInsertRows();
}
}
signals:
void started();
void finished();
void fileFound(const QString &filePath);
private:
int m_limit;
QTimer *m_timer;
QThreadPool *m_threadPool;
};
class FileSystemWorker : public QObject, public QRunnable
{
Q_OBJECT
public:
FileSystemWorker(const QString &path, QObject *parent = nullptr)
: QObject(parent)
, m_path(path)
{
}
void run() override
{
QFileInfoList files = QDir(m_path).entryInfoList(QDir::Files);
foreach (const QFileInfo &fileInfo, files) {
emit fileFound(fileInfo.absoluteFilePath());
}
emit finished();
}
signals:
void started();
void finished();
void fileFound(const QString &filePath);
private:
QString m_path;
};
```
主要的改动有:
1. 在 `CustomSysmodel` 中,将 `FileSystemWorker` 对象改为指针,并在 `fetchFiles` 中动态创建并启动该对象的线程;
2. 将 `FileSystemWorker` 继承自 `QObject`,以便可以发送信号;
3. 将 `FileSystemWorker` 继承自 `QRunnable`,以便可以在线程池中运行;
4. 在 `CustomSysmodel` 中,将 `fileFound` 和 `finished` 信号连接到对应的槽函数上;
5. 在 `CustomSysmodel` 中,将 `QFileSystemModel::fetchMore` 调用的逻辑移动到 `loadMoreFiles` 槽函数中,并在该函数中动态插入新的行。
以上修改方案仅供参考,具体实现可能需要根据实际情况进行调整。
阅读全文