Chinaunix

标题: QT使用QSortFilterProxyModel来做model的过滤 [打印本页]

作者: boy11-2    时间: 2011-02-09 16:34
标题: QT使用QSortFilterProxyModel来做model的过滤
笔者的项目使用QFileSystemModel来展示系统文件目录结构,用户提出新要求要屏蔽掉一些以~,#开头的临时文件

在考察过QFileSystemModel::setFilter无法满足要求之后,笔者引入了QSortFilterProxyModel,使用方法如下:
  1. _model=new QFileSystemModel(this);

  2. _proxyModel = new QSortFilterProxyModel;

  3. _proxyModel->setFilterRegExp("^[^#~].+");

  4. _proxyModel->setSourceModel(_model);

  5. _fileView->setModel(_proxyModel);

  6. QModelIndex rootIndex=_model->setRootPath("My Computer");

  7. _fileView->setRootIndex(_proxyModel->mapFromSource(rootIndex));
复制代码
  1. //以后使用到index的地方需要:

  2. _proxyModel->mapToSource(_fileView->indexAt(pos));
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2