QTreeview model数据过滤:大小写区分,对列过滤,对子对象过滤
示例:
需要自定义QSortFilterProxyModel,
//自定义过滤model
mProxyModel = new TreeFilterProxyModel(this);
//添加treeview当前model
mProxyModel->setSourceModel(model);
//设置过滤行
mProxyModel->setRxCol(col);
//设置过滤行
ui.treeView_3->setModel(mProxyModel);
对某一列过滤
void QTreeViewDemo::filterColumnChanged()
{
QString str = ui.comboBox_2->currentText();
int col = str.toInt();
mProxyModel->setRxCol(col);
mProxyModel->setFilterKeyColumn(col);
ui.treeView_3->expandAll();
}
大小写过滤
void QTreeViewDemo::filterRegExpChanged()
{
QString text = ui.lineEdit->text();
QRegExp::PatternSyntax syntax = QRegExp::PatternSyntax(ui.comboBox->currentData().toInt());
Qt::CaseSensitivity caseSensitivity = ui.checkBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;//Qt::CaseSensitive区分大小写 Qt::CaseInSensitive不区分大小写
QRegExp regExp(text, Qt::CaseInsensitive, syntax);
mProxyModel->setCase(caseSensitivity);
mProxyModel->setRxText(text);
mProxyModel->setFilterRegExp(regExp);
ui.treeView_3->expandAll();
}