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; }; 文件加载页面只有等全部文件加载完才能运行,分析原因,给出修改方案
时间: 2024-02-21 12:58:41 浏览: 156
原因是在加载文件时,使用了阻塞操作,导致界面卡死。解决方案是使用异步加载方式,将文件加载放在后台线程中进行,避免阻塞主线程。
修改方案如下:
1. 将文件加载操作放在后台线程中进行,使用QThreadPool来管理线程池。
2. 在CustomSysmodel的构造函数中,创建定时器m_timer,并设置定时器间隔为2000毫秒。
3. 在CustomSysmodel的fetchFiles函数中,创建FileSystemWorker对象,并将其放入线程池中运行,同时连接FileSystemWorker对象的fileFound信号和CustomSysmodel的fileFound槽函数,以便在后台线程中加载完文件后通知主线程。
4. 在CustomSysmodel的loadMoreFiles槽函数中,检查是否有新文件加载,如果有,则使用beginInsertRows和endInsertRows函数插入新行。
5. 在CustomSysmodel的构造函数中,连接m_timer的timeout信号和CustomSysmodel的loadMoreFiles槽函数,以定时检查是否有新文件需要加载。
修改后的代码如下:
```
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;
};
```
阅读全文