QSortFilterProxyModel使用setFilterRegExp过滤除了名称为AAA和BBB的项,不重写实现
时间: 2024-03-02 15:53:38 浏览: 25
可以通过使用QSortFilterProxyModel的setFilterRegExp函数来实现在QTreeView中使用setFilterRegExp过滤除了名称为AAA和BBB的项的功能,而不需要重写filterAcceptsRow函数。具体操作如下:
1. 在使用QTreeView之前,将其model设置为QSortFilterProxyModel:
```
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(treeView->model());
treeView->setModel(proxyModel);
```
在上述代码中,首先创建了一个QSortFilterProxyModel,并将其源模型设置为QTreeView的模型。然后将QTreeView的模型设置为该QSortFilterProxyModel。
2. 在需要过滤的时候,调用QSortFilterProxyModel的setFilterRegExp函数:
```
QRegExp filterRegExp("(?!AAA|BBB).*", Qt::CaseInsensitive, QRegExp::RegExp);
proxyModel->setFilterRegExp(filterRegExp);
```
在上述代码中,首先创建了一个QRegExp对象,并传入一个正则表达式字符串参数,该正则表达式表示匹配任何不以“AAA”或“BBB”开头的字符串。然后将该正则表达式对象传递给QSortFilterProxyModel的setFilterRegExp函数即可实现过滤除了名称为AAA和BBB的项的功能。
这样,在QTreeView中使用setFilterRegExp函数时,QSortFilterProxyModel会自动根据设置的正则表达式进行过滤,实现过滤除了名称为AAA和BBB的项的功能。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)