1. QSortFilterProxyModel不能单独使用,它只是一个“代理”,真正的数据需要另外的一个model提供,而且它是用来排序和过滤的。

2. 实现代码

#ifndef DIALOG_H
#define DIALOG_H #include <QDialog>
#include <QtGui> class Dialog : public QDialog
{
Q_OBJECT public:
Dialog(QWidget *parent = 0); public slots:
void reapplyFilter(); private:
QSortFilterProxyModel *proxyModel;
QStringListModel *sourceModel;
QListView *listView;
QComboBox *syntaxComboBox;
QLineEdit *filterLineEdit;
}; #endif // DIALOG_H

dialog.cpp

#include "dialog.h"

Dialog::Dialog(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);
syntaxComboBox->addItem("Wildcard", QRegExp::Wildcard);
syntaxComboBox->addItem("Fixed string", QRegExp::Wildcard); 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()));
} void Dialog::reapplyFilter()
{
QRegExp::PatternSyntax syntax = QRegExp::PatternSyntax(syntaxComboBox->itemData(
syntaxComboBox->currentIndex()).toInt());
QRegExp regExp(filterLineEdit->text(), Qt::CaseInsensitive, syntax);
proxyModel->setFilterRegExp(regExp);
}

Qt QSortFilterProxyModel示例代码, 使用方法的更多相关文章

  1. QT QStringListModel 示例代码

    1.  QStringListModel , 实现 插入 删除 编辑 list,支持鼠标双击编辑. 2. dialog.h #ifndef DIALOG_H #define DIALOG_H #inc ...

  2. PyQt(Python+Qt)学习随笔:工具箱(QToolBox)编程使用的步骤及示例代码

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 使用toolBox开发应用时,通过Designer设计ui界面时,只能在Designer中设计too ...

  3. php示例代码之类似于C#中的String.Format方法

    php示例代码之类似于C#中的String.Format方法 原文来自于  http://stackoverflow.com/questions/1241177/c-string-format-equ ...

  4. My.Ioc 代码示例——属性和方法注入

    在 My.Ioc 中,我们可以指定让容器在构建好对象实例之后,自动为我们调用对象的公共方法或是为对象的公共属性赋值.在解析对象实例时,容器将根据我们在注册对象时指定的方法调用或属性赋值的先后顺序,调用 ...

  5. 重新 java 对象的 equals 和 hashCode 方法的建议和示例代码

    equals 方法 equals 方法需要满足的规范: 自反性: 对于任意非空引用 x, x.equals(x) 应该返回 true; 对称性: 对于任意引用, 当且仅当 x.equals(y) == ...

  6. C#使用互斥量(Mutex)实现多进程并发操作时多进程间线程同步操作(进程同步)的简单示例代码及使用方法

    本文主要是实现操作系统级别的多进程间线程同步(进程同步)的示例代码及测试结果.代码经过测试,可供参考,也可直接使用. 承接上一篇博客的业务场景[C#使用读写锁三行代码简单解决多线程并发写入文件时线程同 ...

  7. Xamarin官方示例代码无法部署,提示已跳过部署解决方法

    最近利用Visual Studio 2017学习Android开发.主要是通过Xamarin官方的文档进行的.官方的入门指导提供了很多的示例代码.但是下载之后,调试运行的时候,总是无法部署到虚拟机上. ...

  8. 基于DotNetOpenAuth的OAuth实现示例代码: 获取access token

    1. 场景 根据OAuth 2.0规范,该场景发生于下面的流程图中的(D)(E)节点,根据已经得到的authorization code获取access token. 2. 实现环境 DotNetOp ...

  9. 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题

    调用同步锁的wait().notify().notifyAll()进行线程通信 看这个经典的存取款问题,要求两个线程存款,两个线程取款,账户里有余额的时候只能取款,没余额的时候只能存款,存取款金额相同 ...

随机推荐

  1. [转]Linux进程通信之POSIX消息队列

    进程间的消息队列可以用这个实现,学习了下. http://blog.csdn.net/anonymalias/article/details/9799645?utm_source=tuicool&am ...

  2. 【BZOJ1058】[ZJOI2007]报表统计 STL

    [BZOJ1058][ZJOI2007]报表统计 Description 小Q的妈妈是一个出纳,经常需要做一些统计报表的工作.今天是妈妈的生日,小Q希望可以帮妈妈分担一些工作,作为她的生日礼物之一.经 ...

  3. 【BZOJ2064】分裂 状压DP

    [BZOJ2064]分裂 Description 背景:和久必分,分久必和...题目描述:中国历史上上分分和和次数非常多..通读中国历史的WJMZBMR表示毫无压力.同时经常搞OI的他把这个变成了一个 ...

  4. python清除字符串中间空格的方法

    1.使用字符串函数replace >>> a = 'hello world' >>> a.replace(' ', '') 'helloworld' 看上这种方法真 ...

  5. a database of all existing files

    mlocate.db(5): mlocate database - Linux man page  https://linux.die.net/man/5/mlocate.db Name mlocat ...

  6. camke GUI工具 选择 vs2017 时,如何指定工具集 v140 而不是默认的 v141?

    在参数位置加入 v140 即可,不需要加 -T

  7. [HEOI2014]南园满地堆轻絮

    [HEOI2014]南园满地堆轻絮 BZOJ luogu 二分答案贪心check 首先b[1]最小一定优 之后就贪心的最小化b[i]就行 #include<bits/stdc++.h> u ...

  8. PAT 1067. 试密码(20)

    当你试图登录某个系统却忘了密码时,系统一般只会允许你尝试有限多次,当超出允许次数时,账号就会被锁死.本题就请你实现这个小功能. 输入格式: 输入在第一行给出一个密码(长度不超过20的.不包含空格.Ta ...

  9. python爬虫--爬取cctv连续剧

    #encoding=utf-8 import requests from bs4 import BeautifulSoup import re import os from aria2rpc impo ...

  10. MCU与FPGA通信

    1.MCU启动FPGA相应功能模块 通过译码器选择相应的功能模块,调用实现功能. 2.MCU与FPGA串口通信 SPI协议简单.可靠.易实现,速度快,推荐使用SPI.SPI为四线机制,包含MOSI.M ...