sas*_*alm 10 c++ qt qsortfilterproxymodel
有没有办法使QSortFilterProxyModel中的过滤器无效,但是为了表明过滤器已经缩小,所以filterAcceptsRow()应该只在当前可见的行上调用?
目前Qt不这样做.当我调用QSortFilterProxyModel::invalidateFilter(),并且我的过滤器从"abcd"更改为"abcde"时,会创建一个全新的映射,并filterAcceptsRow()在所有源行上调用,即使很明显到目前为止隐藏的源行仍将保持隐藏状态.
这是来自Qt源代码的代码,QSortFilterProxyModelPrivate::create_mapping()其中调用了我的重写filterAcceptsRow(),它创建了一个全新的Mapping并遍历所有源行:
Mapping *m = new Mapping;
int source_rows = model->rowCount(source_parent);
m->source_rows.reserve(source_rows);
for (int i = 0; i < source_rows; ++i) {
if (q->filterAcceptsRow(i, source_parent))
m->source_rows.append(i);
}
Run Code Online (Sandbox Code Playgroud)
我想要的是只迭代映射中的可见行并filterAcceptsRow()仅调用它们.如果已经隐藏了一行filterAcceptsRow()不应该被调用,因为我们已经知道它会为它返回false(过滤器变得更加严格,它还没有被松开).
由于我已经覆盖filterAcceptsRow(),Qt无法知道过滤器的性质,但是当我打电话时QSortFilterProxyModel::invalidateFilter(),我有关于过滤器是否变得严格变窄的信息,所以我可以将该信息传递给Qt,如果它有办法接受它.
另一方面,如果我将过滤器更改abcd为abce,则应在所有源行上调用过滤器,因为它已经变得非常窄.
我写了一个QIdentityProxyModel子类来存储链接列表QSortFilterProxyModel.它提供了一个类似于QSortFilterProxyModel并接受narrowedDown布尔参数的接口,该参数指示过滤器是否正在缩小.以便:
QSortFilterProxyModel会在链中附加一个new ,并在链QIdentityProxyModel的末尾代替新过滤器.QIdentityProxyModel交换机代理链中的新过滤器.这是一个程序,它将类与使用普通QSortFilterProxyModel子类进行比较:
#include <QtWidgets>
class FilterProxyModel : public QSortFilterProxyModel{
public:
explicit FilterProxyModel(QObject* parent= nullptr):QSortFilterProxyModel(parent){}
~FilterProxyModel(){}
//you can override filterAcceptsRow here if you want
};
//the class stores a list of chained FilterProxyModel and proxies the filter model
class NarrowableFilterProxyModel : public QIdentityProxyModel{
Q_OBJECT
//filtering properties of QSortFilterProxyModel
Q_PROPERTY(QRegExp filterRegExp READ filterRegExp WRITE setFilterRegExp)
Q_PROPERTY(int filterKeyColumn READ filterKeyColumn WRITE setFilterKeyColumn)
Q_PROPERTY(Qt::CaseSensitivity filterCaseSensitivity READ filterCaseSensitivity WRITE setFilterCaseSensitivity)
Q_PROPERTY(int filterRole READ filterRole WRITE setFilterRole)
public:
explicit NarrowableFilterProxyModel(QObject* parent= nullptr):QIdentityProxyModel(parent), m_filterKeyColumn(0),
m_filterCaseSensitivity(Qt::CaseSensitive), m_filterRole(Qt::DisplayRole), m_source(nullptr){
}
void setSourceModel(QAbstractItemModel* sourceModel){
m_source= sourceModel;
QIdentityProxyModel::setSourceModel(sourceModel);
for(FilterProxyModel* proxyNode : m_filterProxyChain) delete proxyNode;
m_filterProxyChain.clear();
applyCurrentFilter();
}
QRegExp filterRegExp()const{return m_filterRegExp;}
int filterKeyColumn()const{return m_filterKeyColumn;}
Qt::CaseSensitivity filterCaseSensitivity()const{return m_filterCaseSensitivity;}
int filterRole()const{return m_filterRole;}
void setFilterKeyColumn(int filterKeyColumn, bool narrowedDown= false){
m_filterKeyColumn= filterKeyColumn;
applyCurrentFilter(narrowedDown);
}
void setFilterCaseSensitivity(Qt::CaseSensitivity filterCaseSensitivity, bool narrowedDown= false){
m_filterCaseSensitivity= filterCaseSensitivity;
applyCurrentFilter(narrowedDown);
}
void setFilterRole(int filterRole, bool narrowedDown= false){
m_filterRole= filterRole;
applyCurrentFilter(narrowedDown);
}
void setFilterRegExp(const QRegExp& filterRegExp, bool narrowedDown= false){
m_filterRegExp= filterRegExp;
applyCurrentFilter(narrowedDown);
}
void setFilterRegExp(const QString& filterRegExp, bool narrowedDown= false){
m_filterRegExp.setPatternSyntax(QRegExp::RegExp);
m_filterRegExp.setPattern(filterRegExp);
applyCurrentFilter(narrowedDown);
}
void setFilterWildcard(const QString &pattern, bool narrowedDown= false){
m_filterRegExp.setPatternSyntax(QRegExp::Wildcard);
m_filterRegExp.setPattern(pattern);
applyCurrentFilter(narrowedDown);
}
void setFilterFixedString(const QString &pattern, bool narrowedDown= false){
m_filterRegExp.setPatternSyntax(QRegExp::FixedString);
m_filterRegExp.setPattern(pattern);
applyCurrentFilter(narrowedDown);
}
private:
void applyCurrentFilter(bool narrowDown= false){
if(!m_source) return;
if(narrowDown){ //if the filter is being narrowed down
//instantiate a new filter proxy model and add it to the end of the chain
QAbstractItemModel* proxyNodeSource= m_filterProxyChain.empty()?
m_source : m_filterProxyChain.last();
FilterProxyModel* proxyNode= newProxyNode();
proxyNode->setSourceModel(proxyNodeSource);
QIdentityProxyModel::setSourceModel(proxyNode);
m_filterProxyChain.append(proxyNode);
} else { //otherwise
//delete all filters from the current chain
//and construct a new chain with the new filter in it
FilterProxyModel* proxyNode= newProxyNode();
proxyNode->setSourceModel(m_source);
QIdentityProxyModel::setSourceModel(proxyNode);
for(FilterProxyModel* node : m_filterProxyChain) delete node;
m_filterProxyChain.clear();
m_filterProxyChain.append(proxyNode);
}
}
FilterProxyModel* newProxyNode(){
//return a new child FilterModel with the current properties
FilterProxyModel* proxyNode= new FilterProxyModel(this);
proxyNode->setFilterRegExp(filterRegExp());
proxyNode->setFilterKeyColumn(filterKeyColumn());
proxyNode->setFilterCaseSensitivity(filterCaseSensitivity());
proxyNode->setFilterRole(filterRole());
return proxyNode;
}
//filtering parameters for QSortFilterProxyModel
QRegExp m_filterRegExp;
int m_filterKeyColumn;
Qt::CaseSensitivity m_filterCaseSensitivity;
int m_filterRole;
QAbstractItemModel* m_source;
QList<FilterProxyModel*> m_filterProxyChain;
};
//Demo program that uses the class
//used to fill the table with dummy data
std::string nextString(std::string str){
int length= str.length();
for(int i=length-1; i>=0; i--){
if(str[i] < 'z'){
str[i]++; return str;
} else str[i]= 'a';
}
return std::string();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//set up GUI
QWidget w;
QGridLayout layout(&w);
QLineEdit lineEditFilter;
lineEditFilter.setPlaceholderText("filter");
QLabel titleTable1("NarrowableFilterProxyModel:");
QTableView tableView1;
QLabel labelTable1;
QLabel titleTable2("FilterProxyModel:");
QTableView tableView2;
QLabel labelTable2;
layout.addWidget(&lineEditFilter,0,0,1,2);
layout.addWidget(&titleTable1,1,0);
layout.addWidget(&tableView1,2,0);
layout.addWidget(&labelTable1,3,0);
layout.addWidget(&titleTable2,1,1);
layout.addWidget(&tableView2,2,1);
layout.addWidget(&labelTable2,3,1);
//set up models
QStandardItemModel sourceModel;
NarrowableFilterProxyModel filterModel1;;
tableView1.setModel(&filterModel1);
FilterProxyModel filterModel2;
tableView2.setModel(&filterModel2);
QObject::connect(&lineEditFilter, &QLineEdit::textChanged, [&](QString newFilter){
QTime stopWatch;
newFilter.prepend("^"); //match from the beginning of the name
bool narrowedDown= newFilter.startsWith(filterModel1.filterRegExp().pattern());
stopWatch.start();
filterModel1.setFilterRegExp(newFilter, narrowedDown);
labelTable1.setText(QString("took: %1 msecs").arg(stopWatch.elapsed()));
stopWatch.start();
filterModel2.setFilterRegExp(newFilter);
labelTable2.setText(QString("took: %1 msecs").arg(stopWatch.elapsed()));
});
//fill model with strings from "aaa" to "zzz" (17576 rows)
std::string str("aaa");
while(!str.empty()){
QList<QStandardItem*> row;
row.append(new QStandardItem(QString::fromStdString(str)));
sourceModel.appendRow(row);
str= nextString(str);
}
filterModel1.setSourceModel(&sourceModel);
filterModel2.setSourceModel(&sourceModel);
w.show();
return a.exec();
}
#include "main.moc"
Run Code Online (Sandbox Code Playgroud)
true参数时narrowedDown,假定过滤器是当前过滤器的特殊情况(即使它实际上并非如此).否则,它的行为与正常情况完全相同,QSortFilterProxyModel并可能带来一些额外的开销(由清理旧的过滤器链产生).QLineEdit(例如,当过滤器从更改回来"abcd"时"abc",因为您应该已经在链中使用过滤器),这可能特别有用"abc".但目前,这还没有实现,因为我希望答案尽可能简洁明了.| 归档时间: |
|
| 查看次数: |
2029 次 |
| 最近记录: |