版权声明:
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处
、作者信息和本声明。否则将追究法律责任。http://devbean.blog.51cto.com/448512/266500
Qt
为我们预定义了很多model,前面已经说过了QStringListModel、QDirModel(也算是Qt推荐使用的
QFileSystemModel吧,这个在上一章最后重新加上了一段话,没有注意的朋友去看看哦)。今天我们要说的这个
QSortFilterProxyModel并不能单独使用,看它的名字就会知道,它只是一个“代理”,真正的数据需要另外的一个model提供,并且它
是用来排序和过滤的。所谓过滤,也就是说按照你输入的内容进行数据的筛选,很像Excel里面的过滤器。不过Qt提供的过滤功能是基于正则表达式的,因而
功能强大。
我们从代码开始看起:
sortview.h






















sortview.cpp












































至于main()函数的内容,由于和前面的代码几乎是一样的,这里就不再贴出来了。我们使用的是QColor::colorNames()函数
提供的数据。这个函数返回值是一个QStringList类型的变量,可以给出预定义的颜色的名字。我们使用一个QStringListModel包装这
个数据,这和前面的内容没有什么区别。然后创建一个QSortFilterProxyModel对象,使用它的setSourceModel()函数将前
面定义的QStringListModel传进去,也就是我们需要对这个model进行代理。那么我们需要过滤哪一列呢?虽然
QStringListModel只有一列,但是我们也需要使用setFilterKeyColumn()函数设置一下,以便让这个proxy知道要过滤
的是第0列。最后重要的一点是,QListView的model必须设置为QSortFilterProxyModel,否则是看不到效果的。
下面的QLineEdit提供过滤数据的输入,这个没什么好说的。后面的QComboBox列出了三项:



这是正则表达式的类型。正则表达式有一套通用的语法,但是对于不同的环境,正则表达式的规则可能是不一样的。第一个
QregExp::RegExp提供了最一般的正则表达式语法,不过这个语法不支持贪婪限定符。这也是Qt默认的规则。如果你需要使用贪婪限定符,需要使
用QRegExp::RegExp2,根据文档描述,这个将会是 Qt5
的默认规则。第二个是Unix下shell很常见的一种规则。第三个即固定表达式,也就是说基本上不使用正则表达式的。
我们使用connect()函数,将QLineEdit的textChanged()信号同slot连接起来。其中我们的slot函数如下所示:







第一步,使用QComboBox的选择值创建一个QRegExp::PatternSyntax对象,然后利用这个语法规则构造一个正则表达
式,注意我们在QLineEdit里面输入的内容是通过参数传递进来的,然后设置proxy的过滤器的表达式。好了,就这样运行一下看看效果吧!

本文出自 “豆子空间 ” 博客,请务必保留此出处http://devbean.blog.51cto.com/448512/266500