参考<<C++ GUI Programming with Qt4>>中文版第二版中的例子"ColorNamesDialog",简单介绍QSortFilterProxyModel的用法,QSortFilterProxyModel不能单独使用,它只是一个“代理”,真正的数据需要另外的一个model提供,而且它是用来排序和过滤的。
colornamesdialog.h文件:
#ifndef COLORNAMESDIALOG_H
#define COLORNAMESDIALOG_H
#include <QtGui/QDialog>
#include <QSortFilterProxyModel>
#include <QStringListModel>
#include <QListView>
#include <QComboBox>
#include <QLineEdit>
class ColorNamesDialog : public QDialog
{
Q_OBJECT
public:
ColorNamesDialog(QWidget *parent = 0);
~ColorNamesDialog();
private slots:
void reapplyFilter();
private:
QSortFilterProxyModel *proxyModel;
QStringListModel *sourceModel;
QListView *listView;
QComboBox *syntaxComboBox;
QLineEdit *filterLineEdit;
};
#endif
colornamesdialog.cpp文件:
#include "colornamesdialog.h"
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
ColorNamesDialog::ColorNamesDialog(QWidget *parent)
: QDialog(parent)
{
sourceModel = new QStringListModel(this);
sourceModel->setStringList(QColor::colorNames());
proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(sourceModel);
proxyModel->setFilterKeyColumn(0);
listView = new QListView;
listView->setModel(proxyModel);
QLabel *filterLabel = new QLabel("Filter:", this);
filterLabel->setFixedWidth(100);
QLabel *patternLabel = new QLabel("Pattern syntax:", this);
patternLabel->setFixedWidth(100);
filterLineEdit = new QLineEdit(this);
syntaxComboBox = new QComboBox(this);
syntaxComboBox->addItem("Regular expression", QRegExp::RegExp); // QRegExp::RegExp 为 0
syntaxComboBox->addItem("Wildcard", QRegExp::Wildcard); // QRegExp::Wildcard 为 1
syntaxComboBox->addItem("Fixed string", QRegExp::Wildcard); // QRegExp::Wildcard 为 2
QHBoxLayout *filterLayout = new QHBoxLayout;
filterLayout->addWidget(filterLabel);
filterLayout->addWidget(filterLineEdit);
QHBoxLayout *patternLayout = new QHBoxLayout;
patternLayout->addWidget(patternLabel);
patternLayout->addWidget(syntaxComboBox);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(listView);
mainLayout->addLayout(filterLayout);
mainLayout->addLayout(patternLayout);
setLayout(mainLayout);
connect(syntaxComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(reapplyFilter()));
connect(filterLineEdit, SIGNAL(textChanged(QString)), this, SLOT(reapplyFilter()));
}
ColorNamesDialog::~ColorNamesDialog()
{
}
void ColorNamesDialog::reapplyFilter()
{
QRegExp::PatternSyntax syntax = QRegExp::PatternSyntax(syntaxComboBox->itemData(
syntaxComboBox->currentIndex()).toInt());
QRegExp regExp(filterLineEdit->text(), Qt::CaseInsensitive, syntax);
proxyModel->setFilterRegExp(regExp);
}
main.cpp文件:
#include <QtGui/QApplication>
#include "colornamesdialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ColorNamesDialog w;
w.show();
w.setWindowTitle("QSortFilterProxyModel Demo");
return a.exec();
}
运行界面:
原文链接: https://www.cnblogs.com/venow/archive/2012/10/18/2730049.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/66262
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!