微商城网站建设新闻,机械公司网站源码,邵阳建设银行网站是多少钱,平顶山市住房和城乡建设局网站QSortFilterProxyModel是model的代理#xff0c;不能单独使用#xff0c;真正的数据需要另外的一个model提供#xff0c;它的工鞥呢是对被代理的model(source model)进行排序和过滤。所谓过滤#xff1a;也就是说按着你输入的内容进行数据的筛选#xff0c;因为器过滤功能…QSortFilterProxyModel是model的代理不能单独使用真正的数据需要另外的一个model提供它的工鞥呢是对被代理的model(source model)进行排序和过滤。所谓过滤也就是说按着你输入的内容进行数据的筛选因为器过滤功能是基本正则表达式所以功能强大。QsortFilterProxyModel类用来为model和view之间提供强大的排序和过滤支持。将模型排序或者过滤后在视图上显示,并且无需对模型中的数据进行任何转换也无需对模型在中数据进行修改
1 QSortFilterProxyModel使用提要
使用过滤器需要指定一个数据模型QStandardItemModel作为数据源并且该数据模型无需设置到表对象上 过滤器指定好数据源后设置到表对象上即可正常使用 过滤器不指定过滤列时默认过滤列为0列
2.QSortFilterProxyModel自定义排序
自定义排序需要子类化QsortFilterProxyModel,然后重写lessThan().注意 : 如果重写了lessThan(),那么就不会再调用model的sort方法了. lessThan()使用示例
bool SortFilterProxyModel::lessThan(const QModelIndex source_left, const QModelIndex source_right) const
{//通过当前视图中的index位置获取model中实际的数据QVariant leftData sourceModel()-data(source_left);QVariant rightData sourceModel()-data(source_right);switch ( source_left.column() ){case 0 : //序号,需要判断数字case 3 : //信号ID,需要判断数字return leftData.toInt() rightData.toInt();break;default : //其它,只判断字符串return leftData.toString() rightData.toString();break;}return true; }除了排序外QSortFilterProxyModel还可以用来隐藏与某个过滤器不匹配的项。使用QRegExp对象指定筛选器并将筛选器应用于给定列的每个项的filterRole() 默认情况下为Qt::DisplayRole。QRegExp对象可用于匹配正则表达式、通配符模式或固定字符串。
3.过滤方法1-使用setFilterKeyColumn()过滤列
首先需要通过void QsortFilterProxyModel::setFilterRegExp(const QRegExp ®Exp)来设置FilterProxyModel的过滤器. 然后通过QsortFilterProxyModel::setFilterKeyColumn(int)来过滤某一列.如果要更改大小写匹配,可以通过QsortFilterProxyModel::sortCaseSensitivity()来设置.示例代码如下所示:
QTableView *view new QTableView;
MyItemModel *sourceModel new MyItemModel(this);
QSortFilterProxyModel *proxyModel new QSortFilterProxyModel(this);
proxyModel-setSourceModel(sourceModel); //将model放入代理中
view-setModel(proxyModel); //在视图中安装代理QRegExp regExp(^(-?\\d)(\\.\\d)?$, Qt::CaseSensitive, QRegExp::RegExp);
//通过^(-?\d)(\.\d)?$来匹配整数
proxyModel-setFilterRegExp(regExp); //安装过滤器proxyModel-setFilterKeyColumn(0);
proxyModel-setFilterKeyColumn(2); //将第一列和第三列同时是整数的数据显示出来.每当过滤格式改变,则setFilterRegExp()重新更新过滤器即可. 弊端:但是这样只能与方式显示model,要第一列和第三列公共是整数的才能显示出来,不能实现或方式显示. 所以,如果要使用联合多列过滤,建议使用过滤方法2来实现.
4.过滤方法2-重写filterAcceptsRow成员函数
以实现只要第一列有整数或者第三列有整数的都显示出来为例,首先需要子类化QsortFilterProxyModel类,然后重写filterAcceptsRow()或者filterAcceptsColumn()函数. 由于我们筛选第一列和第三列,列号是明确的,而行号是未知的, 所以我们只重写filterAcceptsRow()函数.
示例代码如下所示:
bool SortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex source_parent) const
{//获取model中实际的数据QString dataColumn1 sourceModel()-index(source_row, 0, source_parent).data(Qt::DisplayRole).toString();QString dataColumn3 sourceModel()-index(source_row, 2, source_parent).data(Qt::DisplayRole).toString();if(dataColumn1.contains(this-filterRegExp())) {return true;}else if(dataColumn3.contains(this-filterRegExp())) {return true;}return false; }然后创建SortFilterProxyModel类时,只需要安装过滤器即可:
SortFilterProxyModel *proxyModel new SortFilterProxyModel();
proxyModel-setSourceModel(sourceModel); //将model放入代理中
treeView-setModel(proxyModel); //在视图中安装代理
proxyModel-setFilterRegExp(^(-?\\d)(\\.\\d)?$); //安装过滤器每当过滤格式改变,则setFilterRegExp()重新更新过滤器即可.
注意事项:
如果过滤方式改变了,比如从过滤第1列变成了过滤第2列,需要调用invalidateFilter()函数,使之前的过滤失效,激活当前过滤.
setSourceModel用于设置哪个model被代理
setSortCaseSensitivity用来设置排序时是否区分大小写
setFilterKeyColumn用来制定当前过滤的列参数为列好
setFiliterRegExp用于设置过滤时的筛选规则参数类型为QRegExpHardwareLogWidget.h
class CHardwareLogWidget : public QWidget{Q_OBJECTpublic:CHardwareLogWidget(QWidget *parent Q_NULLPTR);~CHardwareLogWidget();protected:void showEvent(QShowEvent *event);private slots:// 重置筛选条件void onReset();// 查询void onQuery();// 导出void onExport();private:// 初始化原始modelvoid initialModel();std::vectorHardWareOperateLogStr getFilterLog();private:Ui::CHardwareLogWidget ui;CLogProxyModel *m_pLogProxyModel{ nullptr }; // 数据模型对象指针QStandardItemModel *m_pRrecordModel{ nullptr }; // 数据模型对象指针};HardwareLogWidget.cpp CHardwareLogWidget::CHardwareLogWidget(QWidget *parent): QWidget(parent)
{ui.setupUi(this);connect(ui.m_pBtnReset, SIGNAL(clicked()), this, SLOT(onReset()));connect(ui.m_pBtnQuery, SIGNAL(clicked()), this, SLOT(onQuery())); connect(ui.m_pBtnExport, SIGNAL(clicked()), this, SLOT(onExport()));ui.m_pLineEditUserName-setValidator(new QRegExpValidator(QRegExp([a-zA-Z0-9]{1,16})));// 弹出日历的形式ui.dateTimeEdit-setCalendarPopup(true);ui.dateTimeEdit_2-setCalendarPopup(true);// 全部/添加用户/删除用户...ui.m_pComboType-addItem(tr(All));for (int i UserAdd; i EquipmentModify; i){ui.m_pComboType-addItem(CLogTypeConverter::logtype2String(static_castHardWareOperateType(i)));}// 设置的选择0好索引ui.m_pComboType-setCurrentIndex(0);//数据模型QStandardItemModel//建立数据模型对象空间并指定父对象// 设置以项数据item data为基础的标准数据模型m_pRrecordModel new QStandardItemModel(this);// 添加列标题m_pRrecordModel-setHorizontalHeaderItem(0, new QStandardItem(tr(Operate Time)));m_pRrecordModel-setHorizontalHeaderItem(1, new QStandardItem(tr(User name)));m_pRrecordModel-setHorizontalHeaderItem(2, new QStandardItem(tr(Operate Type)));m_pRrecordModel-setHorizontalHeaderItem(3, new QStandardItem(tr(Operate Detail)));for (size_t i 0; i 4; i){m_pRrecordModel-horizontalHeaderItem(i)-setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);}//过滤器模型QSortFilterProxyModel//建立过滤器模型对象空间并指定父对象// 设置CLogProxyModel::QSortFilterProxyModel代理m_pLogProxyModel new CLogProxyModel(this);// 指定过滤器模型的数据源模型m_pLogProxyModel-setSourceModel(m_pRrecordModel);// 指定初始化过滤列m_pFilterModel-setFilterKeyColumn(0);//! 将过滤器模型设置到表对象上//! 数据模型就单纯当过滤器模型的数据源即可ui-tableView-setModel(m_pFilterModel);// 获取tableView的水平表头QHeaderView *verticalHeader ui.tableView-verticalHeader();// 使用固定行高大小verticalHeader-setSectionResizeMode(QHeaderView::Fixed);// 设置tableview所有列的默认行高为54verticalHeader-setDefaultSectionSize(54);// 所有对setColumnWidth()的调用都要放在setModel()之后,所有对setColumnWidth()的调用都要放在setModel()之后ui.tableView-setModel(m_pLogProxyModel);// 设置表格列宽ui.tableView-setColumnWidth(0, 300);ui.tableView-setColumnWidth(1, 250);ui.tableView-setColumnWidth(2, 250);// 隐藏表头verticalHeader-hide();// 不显示网格ui.tableView-setShowGrid(false);// 行选择ui.tableView-setSelectionBehavior(QAbstractItemView::SelectRows);// 设置为可以选中多个目标ui.tableView-setSelectionMode(QAbstractItemView::ExtendedSelection);// 设置为不可编辑状态ui.tableView-setEditTriggers(QAbstractItemView::NoEditTriggers);// 设置无交点ui.tableView-setFocusPolicy(Qt::NoFocus);// 初始化initialModel();// 重置onReset();
}
void CHardwareLogWidget::onReset()
{ui.m_pLineEditUserName-clear();m_pLogProxyModel-setFilterKey();ui.m_pComboType-setCurrentIndex(0);m_pLogProxyModel-setFilterLogType(ui.m_pComboType-currentText());// 默认筛选时间为一年内ui.dateTimeEdit_2-setDateTime(QDateTime::currentDateTime());ui.dateTimeEdit-setDateTime(QDateTime::currentDateTime().addYears(-1));m_pLogProxyModel-setDateTimeRange(QDateTime::currentDateTime().addYears(-1), QDateTime::currentDateTime());m_pLogProxyModel-setFilterRegExp(QRegExp());m_pLogProxyModel-setSourceModel(m_pRrecordModel);}////void CHardwareLogWidget::onQuery(){m_pLogProxyModel-setFilterKey(ui.m_pLineEditUserName-text());m_pLogProxyModel-setFilterLogType(ui.m_pComboType-currentText());QDateTime min ui.dateTimeEdit-dateTime();QDateTime max ui.dateTimeEdit_2-dateTime();if (min.toTime_t() max.toTime_t()){QToolTip::showText(ui.dateTimeEdit-mapToGlobal(QPoint(0, 0)), tr(max time must be more than min time));return;}QString str1 min.toString(yyyy-MM-dd hh:mm:ss);m_pLogProxyModel-setDateTimeRange(min, max);m_pLogProxyModel-setFilterRegExp(QRegExp());m_pLogProxyModel-setSourceModel(m_pRrecordModel);}
2 获取过滤器当前选中的文本
获取文本比较简单步骤如下 通过表对象的点击事件能拿到点击位置的QModelIndex对象 使用转到槽/自定义槽函数链接信号槽接收点击信号发出的QModelIndex对象 在槽函数中实现获取当前选中的文本
std::vectorHardWareOperateLogStr CHardwareLogWidget::getFilterLog()
{std::vectorHardWareOperateLogStr vecReturn;int rowCount m_pLogProxyModel-rowCount();for (int i 0; i rowCount; i){HardWareOperateLogStr struLog;// 通过当前视图中的index位置获取model中实际的数据struLog.logTime m_pLogProxyModel-data(m_pLogProxyModel-index(i, 0)).toString();struLog.strUserNumber m_pLogProxyModel-data(m_pLogProxyModel-index(i, 1)).toString();struLog.strOperateType m_pLogProxyModel-data(m_pLogProxyModel-index(i, 2)).toString();struLog.strDetailInfo m_pLogProxyModel-data(m_pLogProxyModel-index(i, 3)).toString();vecReturn.push_back(struLog);}return vecReturn;
}3 修改当前选中item的文本
修改item文本首先需要获取到item对象方法和获取文本一样都需要接收发出点击信号和参数但槽函数内容如下
存放当前点击对象的item指针在.h/.cpp文件中如下定义
4 实现多列分别过滤 20181207151416201 原理重写QSortFilterProxyModel类中的filterAcceptsRow即可
源码如下
mysortfilterproxymodel.h
#ifndef MYSORTFILTERPROXYMODEL_H
#define MYSORTFILTERPROXYMODEL_H#include QSortFilterProxyModel
#include QRegExpclass MySortFilterProxyModel : public QSortFilterProxyModel
{Q_OBJECT
public:MySortFilterProxyModel(QObject *parent 0);void setRxCol1(const QString rx);void setRxCol2(const QString rx);protected:bool filterAcceptsRow(int source_row, const QModelIndex source_parent) const Q_DECL_OVERRIDE;private:QString m_rxCol1;QString m_rxCol2;};#endif // MYSORTFILTERPROXYMODEL_Hwidget.h
#ifndef WIDGET_H
#define WIDGET_H#include QWidgetQT_BEGIN_NAMESPACE
class QStandardItemModel;
class QSortFilterProxyModel;
QT_END_NAMESPACEclass MySortFilterProxyModel;namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QWidget *parent 0);~Widget();protected slots:void col1LineEditChanged(const QString text);void col2LineEidtChanged(const QString text);protected:void intsertModel(const int row, const int col, const QString data);private:Ui::Widget *ui;QStandardItemModel *m_model;MySortFilterProxyModel *m_filterModel;
};#endif // WIDGET_Hmain.cpp
#include widget.h
#include QApplicationint main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}mysortfilterproxymodel.cpp
#include mysortfilterproxymodel.h
#include QModelIndex
#include QDebugMySortFilterProxyModel::MySortFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent)
{m_rxCol1 ;m_rxCol2 ;
}void MySortFilterProxyModel::setRxCol1(const QString rx)
{m_rxCol1 rx;
}void MySortFilterProxyModel::setRxCol2(const QString rx)
{m_rxCol2 rx;
}bool MySortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex source_parent) const
{QModelIndex index0 sourceModel()-index(source_row, 0, source_parent);QModelIndex index1 sourceModel()-index(source_row, 1, source_parent);return (sourceModel()-data(index0).toString().contains(m_rxCol1) sourceModel()-data(index1).toString().contains(m_rxCol2));
}widget.cpp
#include widget.h
#include ui_widget.h
#include mysortfilterproxymodel.h
#include QStandardItemModel
#include QRegExp
#include QDebug
#include QSortFilterProxyModelWidget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui-setupUi(this);this-setWindowTitle(CSDN IT1995);m_model new QStandardItemModel;m_filterModel new MySortFilterProxyModel;QStringList headList;headList 第一列 第二列 第三列 第四列;m_model-setHorizontalHeaderLabels(headList);ui-tableView-horizontalHeader()-setSectionResizeMode(QHeaderView::ResizeToContents);for(int row 0; row 100; row){for(int col 0; col 4; col){intsertModel(row, col, 第 QString::number(row) 行,第 QString::number(col) 列);}}intsertModel(100, 0, 中文);intsertModel(100, 1, China);intsertModel(100, 2, japan);intsertModel(100, 3, 日本人);intsertModel(101, 0, 中文);intsertModel(101, 1, 东京);intsertModel(101, 2, 东京热);intsertModel(101, 3, 东京冷);connect(ui-colOneLineEdit,SIGNAL(textEdited(QString)), this, SLOT(col1LineEditChanged(QString)));connect(ui-colTwoLineEdit,SIGNAL(textEdited(QString)), this, SLOT(col2LineEidtChanged(QString)));m_filterModel-setSourceModel(m_model);ui-tableView-setModel(m_filterModel);
}Widget::~Widget()
{delete ui;
}void Widget::col1LineEditChanged(const QString text)
{m_filterModel-setRxCol1(text);m_filterModel-setSourceModel(m_model);
}void Widget::col2LineEidtChanged(const QString text)
{m_filterModel-setRxCol2(text);m_filterModel-setSourceModel(m_model);
}void Widget::intsertModel(const int row, const int col, const QString data)
{QStandardItem *newItem new QStandardItem(data);newItem-setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);m_model-setItem(row, col, newItem);
}
文章转载自: http://www.morning.hwnnh.cn.gov.cn.hwnnh.cn http://www.morning.dshkp.cn.gov.cn.dshkp.cn http://www.morning.ypjjh.cn.gov.cn.ypjjh.cn http://www.morning.wqfrd.cn.gov.cn.wqfrd.cn http://www.morning.gjwkl.cn.gov.cn.gjwkl.cn http://www.morning.xnzmc.cn.gov.cn.xnzmc.cn http://www.morning.wqpsf.cn.gov.cn.wqpsf.cn http://www.morning.dkzrs.cn.gov.cn.dkzrs.cn http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn http://www.morning.xpzgg.cn.gov.cn.xpzgg.cn http://www.morning.neletea.com.gov.cn.neletea.com http://www.morning.qhvah.cn.gov.cn.qhvah.cn http://www.morning.drnjn.cn.gov.cn.drnjn.cn http://www.morning.rbkgp.cn.gov.cn.rbkgp.cn http://www.morning.tdldh.cn.gov.cn.tdldh.cn http://www.morning.kvzvoew.cn.gov.cn.kvzvoew.cn http://www.morning.ynstj.cn.gov.cn.ynstj.cn http://www.morning.mqmmc.cn.gov.cn.mqmmc.cn http://www.morning.lynkz.cn.gov.cn.lynkz.cn http://www.morning.lmxrt.cn.gov.cn.lmxrt.cn http://www.morning.nnrqg.cn.gov.cn.nnrqg.cn http://www.morning.dsxgc.cn.gov.cn.dsxgc.cn http://www.morning.nlrxh.cn.gov.cn.nlrxh.cn http://www.morning.skwwj.cn.gov.cn.skwwj.cn http://www.morning.bgygx.cn.gov.cn.bgygx.cn http://www.morning.fwnqq.cn.gov.cn.fwnqq.cn http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.rzbgn.cn.gov.cn.rzbgn.cn http://www.morning.qwwhs.cn.gov.cn.qwwhs.cn http://www.morning.clxpp.cn.gov.cn.clxpp.cn http://www.morning.hchrb.cn.gov.cn.hchrb.cn http://www.morning.dhqg.cn.gov.cn.dhqg.cn http://www.morning.smqjl.cn.gov.cn.smqjl.cn http://www.morning.trtxt.cn.gov.cn.trtxt.cn http://www.morning.rrpsw.cn.gov.cn.rrpsw.cn http://www.morning.rlns.cn.gov.cn.rlns.cn http://www.morning.bprsd.cn.gov.cn.bprsd.cn http://www.morning.ngkgy.cn.gov.cn.ngkgy.cn http://www.morning.nfcxq.cn.gov.cn.nfcxq.cn http://www.morning.fflnw.cn.gov.cn.fflnw.cn http://www.morning.wcghr.cn.gov.cn.wcghr.cn http://www.morning.rjrnx.cn.gov.cn.rjrnx.cn http://www.morning.ckhry.cn.gov.cn.ckhry.cn http://www.morning.hxxwq.cn.gov.cn.hxxwq.cn http://www.morning.slpcl.cn.gov.cn.slpcl.cn http://www.morning.qwnqt.cn.gov.cn.qwnqt.cn http://www.morning.qbnfc.cn.gov.cn.qbnfc.cn http://www.morning.rykmf.cn.gov.cn.rykmf.cn http://www.morning.cjnfb.cn.gov.cn.cjnfb.cn http://www.morning.rxtxf.cn.gov.cn.rxtxf.cn http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn http://www.morning.ldpjm.cn.gov.cn.ldpjm.cn http://www.morning.snzgg.cn.gov.cn.snzgg.cn http://www.morning.wrdlf.cn.gov.cn.wrdlf.cn http://www.morning.cspwj.cn.gov.cn.cspwj.cn http://www.morning.grtwn.cn.gov.cn.grtwn.cn http://www.morning.fjfjm.cn.gov.cn.fjfjm.cn http://www.morning.ltqtp.cn.gov.cn.ltqtp.cn http://www.morning.rldph.cn.gov.cn.rldph.cn http://www.morning.ryztl.cn.gov.cn.ryztl.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.nfcxq.cn.gov.cn.nfcxq.cn http://www.morning.qxycf.cn.gov.cn.qxycf.cn http://www.morning.dpflt.cn.gov.cn.dpflt.cn http://www.morning.kjawz.cn.gov.cn.kjawz.cn http://www.morning.jggr.cn.gov.cn.jggr.cn http://www.morning.rrpsw.cn.gov.cn.rrpsw.cn http://www.morning.hympq.cn.gov.cn.hympq.cn http://www.morning.yszrk.cn.gov.cn.yszrk.cn http://www.morning.lffgs.cn.gov.cn.lffgs.cn http://www.morning.pwbps.cn.gov.cn.pwbps.cn http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn http://www.morning.bhbxd.cn.gov.cn.bhbxd.cn http://www.morning.rykgh.cn.gov.cn.rykgh.cn http://www.morning.ywndg.cn.gov.cn.ywndg.cn http://www.morning.rdymd.cn.gov.cn.rdymd.cn http://www.morning.pqkrh.cn.gov.cn.pqkrh.cn http://www.morning.bynf.cn.gov.cn.bynf.cn http://www.morning.rgpy.cn.gov.cn.rgpy.cn http://www.morning.xhlpn.cn.gov.cn.xhlpn.cn