basic sortfilter model Qt基本排序 过滤模式

来源:互联网 发布:hr软件排名 编辑:程序博客网 时间:2024/06/07 11:48

Qt Create 上面的Demo自己在VS2010上面照猫画虎练习熟悉一下API的用法。这个Demo是对QtreeView 加载的数据通过 QSortFilterProxyModel 这个类进行的逻辑排序过滤。

Basic sortfiler model 总共3个文件 一个main.cpp文件 还一个window窗口类 (window.h window.cpp).  

不管写WIN32还是QT的窗口程序都有自己的Main函数。我们先来简单看一下这个简单入口函数都有什么东西;

int main(int argc, char *argv[])
{
      QApplication app(argc, argv);
      Window window;
      window.setSourceModel(createMailModel(&window));
      window.show();
      return app.exec();
}
main函数首先初始化Qt的QApplitcation对象. Window winow;就是我们将要讲述的window类创建的对象。
 setSourceModel是这个对象调用的一个自定义函数用来添加数据。show()设置window窗口显示 最后app.exec()进入主函数循环事件.显示出窗口内容。
下面来主要说一下window这个类看里面具体讲了什么。先把widow.h头文件代码贴一下。

#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
QT_BEGIN_NAMESPACE
class QAbstractItemModel;
class QCheckBox;
class QComboBox;
class QGroupBox;
class QLabel;
class QLineEdit;
class QSortFilterProxyModel;
class QTreeView;
QT_END_NAMESPACE
class Window : public QWidget
{
    Q_OBJECT
public:
    Window();
    void setSourceModel(QAbstractItemModel *model);
private slots:
    void filterRegExpChanged();
    void filterColumnChanged();
    void sortChanged();
private:
    QSortFilterProxyModel *proxyModel;
    QGroupBox *sourceGroupBox;
    QGroupBox *proxyGroupBox;
    QTreeView *sourceView;
    QTreeView *proxyView;
    QCheckBox *filterCaseSensitivityCheckBox;
    QCheckBox *sortCaseSensitivityCheckBox;
    QLabel *filterPatternLabel;
    QLabel *filterSyntaxLabel;
    QLabel *filterColumnLabel;
    QLineEdit *filterPatternLineEdit;
    QComboBox *filterSyntaxComboBox;
    QComboBox *filterColumnComboBox;
};
#endif
这个类用到了Qt的几个控件类 Qlabel QTreeView QLineEdit QComboBox QCheckBox QGroupBox.这些控件类的对象就组成了我们要的界面。每个控件类是做什么用的大家自己去看Qt帮助文档。在这里就不多赘述了。
window类中一个对象调用函数setSourceModel;  把要显示的数据加载到QTreeView界面模型中。
filterRegExpChanged; filterColumnChanged;sortChanged;这三个槽函数是用来接收控件操作发送来的数据处理函数。
QAbstractItemModel这个标准视图模型接收数据进行子类实例化处理。QSortFilterProxyModel用于QTreeView数据的排序过滤逻辑处理。
window.cpp类就是具体的实现了。代码相对比较多。我们就一段一段解析一下。
#include<QtWidgets> //qt窗口类头文件
#include"window.h"  //先把头文件加载进来。
//构造函数
Window::Window()
{
  proxyModel = new QSortFilterProxyModel;//创建排序过滤的逻辑对象
  sourceView = new QTreeView; //原数据树控件创建
  sourceView->setRootIsDecorated(false);//是否设置树控件顶级列表
  sourceView->setAlternatingRowColors(true);//是否交替使用行色

   proxyView = new QTreeView; //这个暂且称之为克隆数据。用来进行排序过滤逻辑处理   proxyView->setRootIsDecorated(false);   proxyView->setAlternatingRowColors(true);   proxyView->setModel(proxyModel); //这里就是把需要进行标准排序过滤的逻辑对象载入   proxyView->setSortingEnabled(true); //是否允许排序设置(就是列表头是否能进行点击进行逻辑排序过滤)
   sortCaseSensitivityCheckBox = new QCheckBox("Case sensitive sorting");  //是否选择排序选项 创建一个CheckBox对象   filterCaseSensitivityCheckBox = new QCheckBox("Case sensitive filter"); //是否选择过滤选项  创建一个CheckBox对象
   filterPatternLineEdit = new QLineEdit; //过滤的关键字输入文本框   filterPatternLabel = new QLabel("&Filter Pattern"); //输入框前面的标题   filterPatternLabel->setBuddy(filterPatternLineEdit);//这里理解算是标题与输入框的一种绑定
 filterSyntaxComboBox = new QComboBox; //储存表达式筛选的信息
 filterSyntaxComboBox->addItem(tr("Regular expression"), QRegExp::RegExp);
 filterSyntaxComboBox->addItem(tr("Wildcard"), QRegExp::Wildcard);
 filterSyntaxComboBox->addItem(tr("Fixed string"), QRegExp::FixedString);
 filterSyntaxLabel = new QLabel(tr("Filter &syntax:"));
 filterSyntaxLabel->setBuddy(filterSyntaxComboBox);//这里也是进行一个标题与ComboBox的绑定
 filterColumnComboBox = new QComboBox; //第二个筛选信息与上面的大同小异用法相同
 filterColumnComboBox->addItem(tr("Subject"));
 filterColumnComboBox->addItem(tr("Sender"));
 filterColumnComboBox->addItem(tr("Date"));
 filterColumnLabel = new QLabel(tr("Filter &column:"));
 filterColumnLabel->setBuddy(filterColumnComboBox);

  //这里就是把输入的关键字以及ComobBox选项用自定义的槽函数进行一个绑定。当你进行控件操作时候都会有数据进行处理
 connect(filterPatternLineEdit, SIGNAL(textChanged(QString)),
            this, SLOT(filterRegExpChanged()));
 connect(filterSyntaxComboBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(filterRegExpChanged()));
 connect(filterColumnComboBox, SIGNAL(currentIndexChanged(int)),
           this, SLOT(filterColumnChanged()));
 connect(filterCaseSensitivityCheckBox, SIGNAL(toggled(bool)),
            this, SLOT(filterRegExpChanged()));
 connect(sortCaseSensitivityCheckBox, SIGNAL(toggled(bool)),
            this, SLOT(sortChanged()));
//QGroupBox就是进行一个界面布局的分布处理(划定边框界限)
 sourceGroupBox = new QGroupBox(tr("Original Model"));
 proxyGroupBox = new QGroupBox(tr("Sorted/Filtered Model"));

 QHBoxLayout *sourceLayout = new QHBoxLayout;
 sourceLayout->addWidget(sourceView); //把原数据QTreeView视图对象导入到布局中
 sourceGroupBox->setLayout(sourceLayout);
//克隆数据的控件载入布局
 QGridLayout *proxyLayout = new QGridLayout;
 proxyLayout->addWidget(proxyView, 0, 0, 1, 3);
 proxyLayout->addWidget(filterPatternLabel, 1, 0);
 proxyLayout->addWidget(filterPatternLineEdit, 1, 1, 1, 2);
 proxyLayout->addWidget(filterSyntaxLabel, 2, 0);
 proxyLayout->addWidget(filterSyntaxComboBox, 2, 1, 1, 2);
 proxyLayout->addWidget(filterColumnLabel, 3, 0);
 proxyLayout->addWidget(filterColumnComboBox, 3, 1, 1, 2);
 proxyLayout->addWidget(filterCaseSensitivityCheckBox, 4, 0, 1, 2);
 proxyLayout->addWidget(sortCaseSensitivityCheckBox, 4, 2);
 proxyGroupBox->setLayout(proxyLayout);

//这里就是把原数据以及克隆数据的分组统一放入主层中分布
 QVBoxLayout *mainLayout = new QVBoxLayout;
 mainLayout->addWidget(sourceGroupBox);
 mainLayout->addWidget(proxyGroupBox);
 setLayout(mainLayout);
 setWindowTitle(tr("Basic Sort/Filter Model"));//窗口标题
 resize(500, 450); //窗口大小
//设置一些属性默认值 启动程序按照下面的规则进行显示
 proxyView->sortByColumn(1, Qt::AscendingOrder);
 filterColumnComboBox->setCurrentIndex(1);
 filterPatternLineEdit->setText("Andy|Grace");
 filterCaseSensitivityCheckBox->setChecked(true);
 sortCaseSensitivityCheckBox->setChecked(true);
}
这就是这个类的构造函数的主要内容。
//自定义函数 这个就是用来加载标准模型数据。通过类对象调用此函数载入想要的内容
void Window::setSourceModel(QAbstractItemModel *model)
{
    proxyModel->setSourceModel(model);
    sourceView->setModel(model);
}
//这个槽函数与关键字输入框相绑定,就是通过变更内容进行数据重新显示
void Window::filterRegExpChanged()
{
    QRegExp::PatternSyntax syntax =
            QRegExp::PatternSyntax(filterSyntaxComboBox->itemData(
                    filterSyntaxComboBox->currentIndex()).toInt());
    Qt::CaseSensitivity caseSensitivity =
            filterCaseSensitivityCheckBox->isChecked() ? Qt::CaseSensitive
                                                       : Qt::CaseInsensitive;
    QRegExp regExp(filterPatternLineEdit->text(), caseSensitivity, syntax);
    proxyModel->setFilterRegExp(regExp);
}
//这个是与ComboBox相绑定的槽函数 选择了第几项
void Window::filterColumnChanged()
{
    proxyModel->setFilterKeyColumn(filterColumnComboBox->currentIndex());
}
//当CheckBox选项进行变更以后就会触发此槽函数,进行排序过滤的逻辑变更
void Window::sortChanged()
{
    proxyModel->setSortCaseSensitivity(
            sortCaseSensitivityCheckBox->isChecked() ? Qt::CaseSensitive
                                                     : Qt::CaseInsensitive);
}

如果理解的上面Window类的内容,那下面就是对这个类进行数据载入显示。在main.cpp这个类里面还有2个函数用来载入数据.
//这里是写了一个通用函数方便数据的传入
void addMail(QAbstractItemModel *model, const QString &subject,
             const QString &sender, const QDateTime &date)
{
    model->insertRow(0);
    model->setData(model->index(0, 0), subject);
    model->setData(model->index(0, 1), sender);
    model->setData(model->index(0, 2), date);
}
//这里就是创建标准模型数据对象,然后由Window类对象把数据载入
QAbstractItemModel *createMailModel(QObject *parent)
{
    QStandardItemModel *model = new QStandardItemModel(0, 3, parent);//定义一个3列的标准模型
    model->setHeaderData(0, Qt::Horizontal, QObject::tr("Subject"));//表头
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("Sender"));
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("Date"));
    //通过调用上面通用的函数把数据载入进来
    addMail(model, "Happy New Year!", "Grace K. <grace@software-inc.com>",
            QDateTime(QDate(2006, 12, 31), QTime(17, 03)));
    addMail(model, "Radically new concept", "Grace K. <grace@software-inc.com>",
            QDateTime(QDate(2006, 12, 22), QTime(9, 44)));
    addMail(model, "Accounts", "pascale@nospam.com",
            QDateTime(QDate(2006, 12, 31), QTime(12, 50)));
    addMail(model, "Expenses", "Joe Bloggs <joe@bloggs.com>",
            QDateTime(QDate(2006, 12, 25), QTime(11, 39)));
    addMail(model, "Re: Expenses", "Andy <andy@nospam.com>",
            QDateTime(QDate(2007, 01, 02), QTime(16, 05)));
    addMail(model, "Re: Accounts", "Joe Bloggs <joe@bloggs.com>",
            QDateTime(QDate(2007, 01, 03), QTime(14, 18)));
    addMail(model, "Re: Accounts", "Andy <andy@nospam.com>",
            QDateTime(QDate(2007, 01, 03), QTime(14, 26)));
    addMail(model, "Sports", "Linda Smith <linda.smith@nospam.com>",
            QDateTime(QDate(2007, 01, 05), QTime(11, 33)));
    addMail(model, "AW: Sports", "Rolf Newschweinstein <rolfn@nospam.com>",
            QDateTime(QDate(2007, 01, 05), QTime(12, 00)));
    addMail(model, "RE: Sports", "Petra Schmidt <petras@nospam.com>",
            QDateTime(QDate(2007, 01, 05), QTime(12, 01)));
    return model;
}
//下面是在VS2010上的运行效果 
就到这里吧!练手......

0 0
原创粉丝点击