QSortFilterProxyModel的简单用法

  参考<<C++ GUI Programming with Qt 4>>中文版第二版中的例子"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();
}

  运行界面:

                                 

QTreeView 是一个用于呈现树形结构数据的控件,QStyledItemDelegate 是一个用于自定义单个单元格显示的代理模型,QAbstractProxyModelQSortFilterProxyModel 则分别是用于过滤和排序数据的代理模型。 使用这些类的一般流程如下: 1. 创建一个数据模型,可以是 QStandardItemModel 或自定义的模型。 2. 如果需要过滤或排序数据,可以创建一个 QAbstractProxyModelQSortFilterProxyModel 的子类,重写相应的方法实现过滤或排序逻辑。 3. 创建一个 QTreeView 实例,调用 setModel() 方法将数据模型设置给 QTreeView。 4. 如果需要自定义单元格显示,可以创建一个 QStyledItemDelegate 的子类,重写相应的方法实现自定义的显示逻辑,并调用 setItemDelegate() 方法将代理模型设置给 QTreeView。 下面是一个简单的例子,展示如何使用 QTreeView 和 QSortFilterProxyModel: ```python from PyQt5.QtCore import Qt, QSortFilterProxyModel from PyQt5.QtGui import QStandardItem, QStandardItemModel from PyQt5.QtWidgets import QApplication, QTreeView class MySortFilterProxyModel(QSortFilterProxyModel): def filterAcceptsRow(self, source_row, source_parent): index = self.sourceModel().index(source_row, 0, source_parent) text = self.sourceModel().data(index, Qt.DisplayRole) return text.startswith("A") if __name__ == "__main__": app = QApplication([]) model = QStandardItemModel() model.setHorizontalHeaderLabels(["Name", "Age"]) for name, age in [("Alice", 25), ("Bob", 30), ("Charlie", 35)]: item = QStandardItem(name) item.setData(age, Qt.DisplayRole) model.appendRow(item) proxy_model = MySortFilterProxyModel() proxy_model.setSourceModel(model) tree_view = QTreeView() tree_view.setModel(proxy_model) tree_view.show() app.exec_() ``` 在这个例子中,我们创建了一个数据模型 QStandardItemModel,其中包含三个行,每行有两列数据,分别是姓名和年龄。然后创建了一个代理模型 MySortFilterProxyModel,重写了 filterAcceptsRow() 方法,只允许以字母 "A" 开头的行通过过滤。最后创建了一个 QTreeView 实例,将代理模型设置给 QTreeView,展示了过滤后的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值