Model/view 第二部分:QFileSystemModel的使用

Model/view 第二部分:QFileSystemModel的使用

QFileSystemModel类的基本功能:

QFileSystemModel提供了一个可用于访问本机文件系统的数据模型,其和视图组件QTreeView结合使用,可以用目录树的形式显示本机上的文件系统

同Windows的资源管理器一样。

通过使用setRootPath()函数为QFileSystemModel设置一个根目录,例如:

QFileSystemModel *model = new QFileSystemModel;

model->setRootPath(QDir::currentPath());

QDir::currentPath()用于获取当前应用程序的当前路径

接下来就做个小项目来实践下

目的:通过路径获取当前系统的结构并且使用TreeView显示出来,在TreeView中点击一个目录时,其目录的下层文件信息在listview和tableview中显示出来。

  1. ui建立:新建三个groupBox,在其内部放入Treeview,ListView,TableView
  2. 代码实现:
    1. 新建一个QFileSystemModel:QFileSystemModel *model = new QFileSystemModel();
  3. MainWindow构造函数中代码如下:

 

                model->setRootPath(QDir::currentPath());//设置根目录

                将数据模型设进个view中:

                ui->treeView->setModel(model);

                ui->listView->setModel(model);

                ui->tableView->setModel(model)

  1. 当treeview被单击时,将tableview和listview的setRootPath重新设置根目录即可。相关的根目录信息包含在QModelIndex中。具体代码如下:

  connect(ui->treeView,SIGNAL(clicked(QModelIndex)),

ui>listView,SLOT(setRootIndex(QModelIndex)));

    connect(ui->treeView,SIGNAL(clicked(QModelIndex)),ui->tableView,SLOT(setRootIndex(QModelIndex)));

运行后的结果如下图所示:

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
class CustomSysmodel : public QFileSystemModel { Q_OBJECT public: CustomSysmodel(QWidget *parent = Q_NULLPTR) : QFileSystemModel(parent) , m_limit(100) , m_timer(new QTimer(this))//m_timer 使用 this 作为其父对象创建的,在 CustomSysmodel 对象被删除时自动删除(不确定) { connect(m_timer, &QTimer::timeout, this, &CustomSysmodel::loadMoreFiles);//直接在构造函数中植入计时器 m_timer->setInterval(2000); } void CustomSysmodel::fetchFiles(const QString &path) { emit started(); QThread *thread = new QThread; //**********************需要释放空间 FileSystemWorker *worker = new FileSystemWorker; worker->moveToThread(thread); connect(thread, &QThread::started, worker, this, worker, path { worker->fetchFiles(path); }); connect(worker, &FileSystemWorker::fileFound, this, &CustomSysmodel::fileFound); connect(worker, &FileSystemWorker::finished, thread, &QThread::quit); connect(worker, &FileSystemWorker::finished, worker, &FileSystemWorker::deleteLater); connect(thread, &QThread::finished, thread, &QThread::deleteLater); connect(worker, &FileSystemWorker::finished, this, &CustomSysmodel::finished); thread->start();//启动线程 QMetaObject::invokeMethod(thread, "wait", Qt::QueuedConnection);//另一种写法,还是不能边构建model边描画 delete thread; delete worker; } } class FileSystemWorker : public QObject { Q_OBJECT public: FileSystemWorker(QObject *parent = nullptr) : QObject(parent) {} public slots: void fetchFiles(const QString &path) { QFileInfoList files = QDir(path).entryInfoList(QDir::Files); foreach (const QFileInfo &fileInfo, files) { cout<<"xianc"<<endl; emit fileFound(fileInfo.absoluteFilePath()); } } signals: void started(); void finished(); void fileFound(const QString &filePath); }; 如上述代码所示:qtreeview使用继承自Qfilesystemmodel的自定义模型,想要实现单独线程读取文件,再发送给主线程,目的是为了访问百万级文件时,可以流畅访问。 但现在只有全部加载完主线程才能运行,分析原因,给出完整的修改方案
05-31

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值