QFileSystemModel:setRootPath导致Segmentation fault

  • 现象:程序在调用QFileSystemModel对象的setRootPath方法后异常退出
  • 原因:main函数中创建的Application对象为QGuiApplication类型
  • 解决方法:将Application对象替换为QApplication类型
  • Qt帮助文档的相关描述(https://doc.qt.io/qt-5/qfilesystemmodel.html)如下图
    在这里插入图片描述
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; }; 文件加载页面只有等全部文件加载完才能运行,分析原因,给出修改方案
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值