Chinaunix
标题:
QT使用QSortFilterProxyModel来做model的过滤
[打印本页]
作者:
boy11-2
时间:
2011-02-09 16:34
标题:
QT使用QSortFilterProxyModel来做model的过滤
笔者的项目使用QFileSystemModel来展示系统文件目录结构,用户提出新要求要屏蔽掉一些以~,#开头的临时文件
在考察过QFileSystemModel::setFilter无法满足要求之后,笔者引入了QSortFilterProxyModel,使用方法如下:
_model=new QFileSystemModel(this);
_proxyModel = new QSortFilterProxyModel;
_proxyModel->setFilterRegExp("^[^#~].+");
_proxyModel->setSourceModel(_model);
_fileView->setModel(_proxyModel);
QModelIndex rootIndex=_model->setRootPath("My Computer");
_fileView->setRootIndex(_proxyModel->mapFromSource(rootIndex));
复制代码
//以后使用到index的地方需要:
_proxyModel->mapToSource(_fileView->indexAt(pos));
复制代码
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2