QSortFilterProxyModel使用setFilterRegExp过滤除了名称为AAA的项,不重写
时间: 2024-03-02 14:53:36 浏览: 25
可以通过使用QSortFilterProxyModel的setFilterFixedString函数来实现在QTreeView中使用setFilterRegExp过滤除了名称为AAA的项的功能,而不需要重写filterAcceptsRow函数。具体操作如下:
1. 在使用QTreeView之前,将其model设置为QSortFilterProxyModel:
```
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(treeView->model());
treeView->setModel(proxyModel);
```
在上述代码中,首先创建了一个QSortFilterProxyModel,并将其源模型设置为QTreeView的模型。然后将QTreeView的模型设置为该QSortFilterProxyModel。
2. 在需要过滤的时候,调用QSortFilterProxyModel的setFilterFixedString函数:
```
proxyModel->setFilterFixedString("AAA");
```
在上述代码中,调用setFilterFixedString函数并传入一个字符串参数,表示需要过滤的字符串。在本例中,传入的字符串为“AAA”,表示需要过滤名称为AAA的项。
这样,在QTreeView中使用setFilterRegExp函数时,QSortFilterProxyModel会自动根据设置的过滤字符串进行过滤,实现过滤除了名称为AAA的项的功能。
相关推荐
![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)