Qt优秀开源项目之十四:SortFilterProxyModel
Posted 草上爬
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt优秀开源项目之十四:SortFilterProxyModel相关的知识,希望对你有一定的参考价值。
一.简介
Widget中要实现model的排序和过滤还是很方便的,直接使用QSortFilterProxyModel即可,而且Qt自带的两个demo——basicsortfiltermodel和customsortfiltermodel讲解了QSortFilterProxyModel的基本使用方法。在QML中如果实现过滤和排序,可以继承QSortFilterProxyModel然后将C++接口暴露给QML使用,可参考:Qt之在QML中使用QSortFilterProxyModel进行排序和过滤,该博客实现了source model使用QML内置model和source model使用自定义model两种方式,虽然实现了功能,但是通用性和灵活性一言难尽。
没想到有大佬开源了一个very nice的项目:SortFilterProxyModel,该项目解决了前面说的通用性和灵活性的问题,使在QML中优雅的使用过滤和排序成为可能
SortFilterProxyModel的github地址:https://github.com/oKcerG/SortFilterProxyModel/
源码不多,我都看了一遍,可以说是麻雀虽小五脏俱全,功能很丰富,能封装的都封装了,看了下issues里有人反馈性能问题,可以这样说如果这个项目有性能问题,那么你自己用QSortFilterProxyModel从头去弄也会有性能问题。
这个项目对QML内置的model支持的非常好,但是对自定义model会有些问题,详见第三节
二.使用
使用也是非常方便
1.在pro中添加include(SortFilterProxyModel/SortFilterProxyModel.pri)
2.在QML中添加import SortFilterProxyModel 0.2
一个简单的demo
import QtQuick 2.2
import QtQuick.Controls 1.2
import SortFilterProxyModel 1.0
ApplicationWindow
visible: true
width: 640
height: 480
ListModel
id: personModel
ListElement firstName: "Adan"; lastName: "Gula"; favorite: true
ListElement firstName: "Alexandria"; lastName: "Armand"; favorite: false
ListElement firstName: "Alverta"; lastName: "Gorney"; favorite: false
ListElement firstName: "Annalisa"; lastName: "Dixion"; favorite: false
ListElement firstName: "Arlene"; lastName: "Drapeau"; favorite: false
ListElement firstName: "Ashely"; lastName: "Euler"; favorite: false
ListElement firstName: "Ashleigh"; lastName: "Terry"; favorite: false
ListElement firstName: "Audra"; lastName: "Vannorman"; favorite: false
ListElement firstName: "Bibi"; lastName: "Fraire"; favorite: false
ListElement firstName: "Branda"; lastName: "Melgoza"; favorite: false
ListElement firstName: "Breanna"; lastName: "Rotenberry"; favorite: false
ListElement firstName: "Brigette"; lastName: "Delk"; favorite: false
ListElement firstName: "Bruno"; lastName: "Raso"; favorite: false
ListElement firstName: "Calandra"; lastName: "Dudney"; favorite: true
ListElement firstName: "Carrol"; lastName: "Dedeaux"; favorite: false
ListElement firstName: "Celina"; lastName: "Bichrest"; favorite: false
ListElement firstName: "Celinda"; lastName: "Frutos"; favorite: false
ListElement firstName: "Celsa"; lastName: "Moen"; favorite: false
ListElement firstName: "Charise"; lastName: "Milan"; favorite: true
ListElement firstName: "Corazon"; lastName: "Burrus"; favorite: false
ListElement firstName: "Coreen"; lastName: "Dehner"; favorite: false
ListElement firstName: "Coretta"; lastName: "Stillwell"; favorite: false
ListElement firstName: "Cornell"; lastName: "Fierros"; favorite: false
ListElement firstName: "Deedee"; lastName: "Ruggerio"; favorite: false
ListElement firstName: "Demetrice"; lastName: "Mcclaren"; favorite: false
ListElement firstName: "Desiree"; lastName: "Marcano"; favorite: false
ListElement firstName: "Dimple"; lastName: "Harig"; favorite: false
ListElement firstName: "Divina"; lastName: "Fahie"; favorite: true
ListElement firstName: "Don"; lastName: "Thill"; favorite: false
ListElement firstName: "Ellena"; lastName: "Fredrick"; favorite: false
ListElement firstName: "Elmo"; lastName: "Routh"; favorite: false
ListElement firstName: "Elsie"; lastName: "Raulerson"; favorite: false
ListElement firstName: "Ervin"; lastName: "Bardsley"; favorite: false
ListElement firstName: "Estela"; lastName: "Alverson"; favorite: false
ListElement firstName: "Eufemia"; lastName: "Sheely"; favorite: true
ListElement firstName: "Fawn"; lastName: "Opie"; favorite: false
ListElement firstName: "Fred"; lastName: "Ralphs"; favorite: false
ListElement firstName: "Genevie"; lastName: "Spires"; favorite: false
ListElement firstName: "Genna"; lastName: "Quin"; favorite: false
ListElement firstName: "Gerri"; lastName: "Aden"; favorite: false
ListElement firstName: "Gianna"; lastName: "Shears"; favorite: false
ListElement firstName: "Gilda"; lastName: "Doyel"; favorite: false
ListElement firstName: "Glady"; lastName: "Peguero"; favorite: false
ListElement firstName: "Guy"; lastName: "Sondag"; favorite: false
ListElement firstName: "Herschel"; lastName: "Flowers"; favorite: false
ListElement firstName: "Hilda"; lastName: "Hamm"; favorite: false
ListElement firstName: "Hiroko"; lastName: "Mccarley"; favorite: false
ListElement firstName: "Jackeline"; lastName: "Millington"; favorite: false
ListElement firstName: "Jacqualine"; lastName: "Twiss"; favorite: false
ListElement firstName: "Jacquelyne"; lastName: "Worsley"; favorite: false
ListElement firstName: "Janelle"; lastName: "Broman"; favorite: false
ListElement firstName: "Jani"; lastName: "Reddish"; favorite: false
ListElement firstName: "Jasper"; lastName: "Krone"; favorite: false
ListElement firstName: "Jene"; lastName: "Gump"; favorite: false
ListElement firstName: "Jimmie"; lastName: "Cooley"; favorite: false
ListElement firstName: "Kellye"; lastName: "Wiest"; favorite: false
ListElement firstName: "Kristie"; lastName: "Jules"; favorite: false
ListElement firstName: "Kymberly"; lastName: "Moodie"; favorite: false
ListElement firstName: "Latesha"; lastName: "Netter"; favorite: false
ListElement firstName: "Lavelle"; lastName: "Lasala"; favorite: false
ListElement firstName: "Lesli"; lastName: "Czapla"; favorite: false
ListElement firstName: "Lorette"; lastName: "Prescott"; favorite: false
ListElement firstName: "Lorie"; lastName: "Hoeft"; favorite: false
ListElement firstName: "Lyndia"; lastName: "Schwalm"; favorite: false
ListElement firstName: "Mable"; lastName: "Marron"; favorite: false
ListElement firstName: "Marcelino"; lastName: "Spence"; favorite: false
ListElement firstName: "Marcie"; lastName: "Yopp"; favorite: false
ListElement firstName: "Margrett"; lastName: "Spagnuolo"; favorite: false
ListElement firstName: "Maris"; lastName: "Ruff"; favorite: false
ListElement firstName: "Marvella"; lastName: "Bowlby"; favorite: false
ListElement firstName: "Mauro"; lastName: "Winberg"; favorite: false
ListElement firstName: "Maybelle"; lastName: "Chute"; favorite: false
ListElement firstName: "Meghan"; lastName: "Burts"; favorite: false
ListElement firstName: "Micah"; lastName: "Hoggan"; favorite: false
ListElement firstName: "Mireille"; lastName: "Eastin"; favorite: false
ListElement firstName: "Mitsue"; lastName: "Ohler"; favorite: false
ListElement firstName: "Monica"; lastName: "Mccall"; favorite: false
ListElement firstName: "Morris"; lastName: "Cappel"; favorite: false
ListElement firstName: "Myong"; lastName: "Bale"; favorite: false
ListElement firstName: "Nannie"; lastName: "Poore"; favorite: false
ListElement firstName: "Noella"; lastName: "Gledhill"; favorite: false
ListElement firstName: "Ofelia"; lastName: "Bane"; favorite: true
ListElement firstName: "Phoebe"; lastName: "Bui"; favorite: false
ListElement firstName: "Quincy"; lastName: "Sweeting"; favorite: false
ListElement firstName: "Raguel"; lastName: "Ross"; favorite: false
ListElement firstName: "Raymundo"; lastName: "Headlee"; favorite: false
ListElement firstName: "Ressie"; lastName: "Helt"; favorite: false
ListElement firstName: "Rikki"; lastName: "Gilligan"; favorite: false
ListElement firstName: "Roselee"; lastName: "Swain"; favorite: false
ListElement firstName: "Shanda"; lastName: "Ballew"; favorite: false
ListElement firstName: "Stephine"; lastName: "Dimauro"; favorite: false
ListElement firstName: "Tamesha"; lastName: "Roop"; favorite: false
ListElement firstName: "Temple"; lastName: "Mcbain"; favorite: false
ListElement firstName: "Tiesha"; lastName: "Crumley"; favorite: false
ListElement firstName: "Tova"; lastName: "Kiel"; favorite: false
ListElement firstName: "Wade"; lastName: "Fossum"; favorite: false
ListElement firstName: "Willodean"; lastName: "Ferguson"; favorite: false
ListElement firstName: "Winfred"; lastName: "High"; favorite: false
ListElement firstName: "Winter"; lastName: "Furrow"; favorite: false
ListElement firstName: "Yvone"; lastName: "Edge"; favorite: true
TextField
id: textField
anchors top: parent.top; left: parent.left; right: parent.right
height: implicitHeight
SortFilterProxyModel
id: personProxyModel
sourceModel: personModel
filters: RegExpFilter
roleName: "lastName"
pattern: textField.text
caseSensitivity: Qt.CaseInsensitive
sorters: StringSorter roleName: "firstName"
ListView
anchors top: textField.bottom; bottom: parent.bottom; left: parent.left; right: parent.right
model: personProxyModel
delegate: Text text: model.firstName + " " + model.lastName
该demo按照firstName排序,按照lastName过滤来实现搜索,效果如下:
demo稍微有点简陋,而且只用到一个filter和sorter,像AllOf和AnyOf这种容器类型的filter都没用到,而且sorter的种类也很丰富,这里只用到了字符串排序,好在官方提供了一个漂亮的demo—SFPMShowcase
SFPMShowcase的github地址:https://github.com/oKcerG/SFPMShowcase
效果如下:
具体实现可参考其实现
但是如果要真正用好SortFilterProxyModel,源码还是得看的
三.自定义model
如果自定义model中的数据和roleName是一样对应的关系,那么这个自定义model对SortFilterProxyModel来说在使用上和内置model是一样的,下面是一个例子
QHListModel.h
#ifndef QHLISTMODEL_H
#define QHLISTMODEL_H
#include <QAbstractListModel>
enum Roles
IDRole = Qt::UserRole + 1,
NameRole = Qt::UserRole + 2
;
class QHListModel : public QAbstractListModel
public:
QHListModel();
~QHListModel();
void addData(int id, const QString &name);
protected:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames() const override;
private:
QList<int> m_userIDs;
QStringList m_userNames;
;
#endif // QHLISTMODEL_H
QHListModel.cpp
#include "QHListModel.h"
QHListModel::QHListModel()
QHListModel::~QHListModel()
void QHListModel::addData(int id, const QString &name)
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_userIDs.append(id);
m_userNames.append(name);
endInsertRows();
int QHListModel::rowCount(const QModelIndex &parent) const
Q_UNUSED(parent)
return m_userNames.count();
QVariant QHListModel::data(const QModelIndex &index, int role) const
if (index.row() < 0 || index.row() >= m_userNames.count())
return QVariant();
if (role == IDRole)
return m_userIDs[index.row()];
if (role == NameRole)
return m_userNames[index.row()];
return QVariant();
QHash<int, QByteArray> QHListModel::roleNames() const
QHash<int, QByteArray> roles;
roles[IDRole] = "userID";
roles[NameRole] = "userName";
return roles;
这个model中的用户ID和用户名数据相对应的roleName分别是userID和userName,在SortFilterProxyModel中只需操作userID和userName即可
main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import SortFilterProxyModel 0.2
Window
visible: true
width: 480
height: 800
title: qsTr("Hello World")
Item
anchors.fill: parent
anchors.margins: 10
SortFilterProxyModel
id: proxyModel
sourceModel: searchModel
sorters: RoleSorter
roleName: "userID"
sortOrder: searchTool.sortOrder
filters: RegExpFilter
roleName: "userName"
pattern: textField.text
caseSensitivity: Qt.CaseInsensitive
Rectangle
id: searchTool
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
height: 40
property int sortOrder: Qt.AscendingOrder
RowLayout
anchors.fill: parent
anchors.centerIn: parent
TextField
id: textField
placeholderText: "请输入关键字"
Layout.fillWidth: true
font.pointSize: 12
CheckBox
text: "DescendingOrder"
onCheckedChanged:
searchTool.sortOrder = checked ? Qt.DescendingOrder : Qt.AscendingOrder;
ListView
id: view
model: proxyModel
anchors.top: searchTool.bottom
anchors.topMargin: 3
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.right: parent.right
cacheBuffer: 100
clip: true
spacing: 1
delegate: Rectangle
width: parent.width
radius: 5
anchors.horizontalCenter: parent.horizontalCenter
height: 40
color: "black"
RowLayout
anchors.fill: parent
anchors.leftMargin: 30
spacing: 30
Text
text: userID
font.pointSize: 12
font.bold: true
color: "white"
Text
text: userName
font.pointSize: 12
font.bold: true
color: "white"
Layout.fillWidth: true
ScrollBar.vertical: ScrollBar
anchors.right: parent.right
anchors.rightMargin: 1
width: 16
在SortFilterProxyModel中,使用userID进行排序,使用userName进行过滤
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "QHListModel.h"
#pragma execution_character_set("utf-8")
// 随机民生成器.
inline void DataBaseRoll(std::string &name)
//百家姓-单姓.
std::string NA1[444] = "赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许", "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎", "鲁", "韦", "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳", "酆", "鲍", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺", "倪", "汤", "滕", "殷", "罗", "毕", "郝", "邬", "安", "常", "乐", "于", "时", "傅", "皮", "卞", "齐", "康", "伍", "余", "元", "卜", "顾", "孟", "平", "黄", "和", "穆", "萧", "尹", "姚", "邵", "湛", "汪", "祁", "毛", "禹", "狄", "米", "贝", "明", "臧", "计", "伏", "成", "戴", "谈", "宋", "茅", "庞", "熊", "纪", "舒", "屈", "项", "祝", "董", "梁", "杜", "阮", "蓝", "闵", "席", "季", "麻", "强", "贾", "路", "娄", "危", "江", "童", "颜", "郭", "梅", "盛", "林", "刁", "钟", "徐", "邱", "骆", "高", "夏", "蔡", "田", "樊", "胡", "凌", "霍", "虞", "万", "支", "柯", "昝", "管", "卢", "莫", "经", "房", "裘", "缪", "干", "解", "应", "宗", "丁", "宣", "贲", "邓", "郁", "单", "杭", "洪", "包", "诸", "左", "石", "崔", "吉", "钮", "龚", "程", "嵇", "邢", "滑", "裴", "陆", "荣", "翁", "荀", "羊", "於", "惠", "甄", "麴", "家", "封", "芮", "羿", "储", "靳", "汲", "邴", "糜", "松", "井", "段", "富", "巫", "乌", "焦", "巴", "弓", "牧", "隗", "山", "谷", "车", "侯", "宓", "蓬", "全", "郗", "班", "仰", "秋", "仲", "伊", "宫", "宁", "仇", "栾", "暴", "甘", "钭", "厉", "戎", "祖", "武", "符", "刘", "景", "詹", "束", "龙", "叶", "幸", "司", "韶", "郜", "黎", "蓟", "薄", "印", "宿", "白", "怀", "蒲", "邰", "从", "鄂", "索", "咸", "籍", "赖", "卓", "蔺", "屠", "蒙","池", "乔", "阴", "郁", "胥", "能", "苍", "双", "闻", "莘", "党", "翟", "谭", "贡", "劳", "逄", "姬", "申", "扶", "堵", "冉", "宰", "郦", "雍", "舄", "璩", "桑", "桂", "濮", "牛", "寿", "通", "边", "扈", "燕", "冀", "郏", "浦", "尚", "农", "温", "别", "庄", "晏", "柴", "瞿", "阎", "充", "慕", "连", "茹", "习", "宦", "艾", "鱼", "容", "向", "古", "易", "慎", "戈", "廖", "庾", "终", "暨", "居", "衡", "步", "都", "耿", "满", "弘", "匡", "国", "文", "寇", "广", "禄", "阙", "东", "殴", "殳", "沃", "利", "蔚", "越", "夔", "隆", "师", "巩", "厍", "聂", "晁", "勾", "敖", "融", "冷", "訾", "辛", "阚", "那", "简", "饶", "空", "曾", "毋", "沙", "乜", "养", "鞠", "须", "丰", "巢", "关", "蒯", "相", "查", "後", "荆", "红", "游", "竺", "权", "逯", "盖", "益", "桓", "公", "仉", "督", "晋", "楚", "闫", "法", "汝", "鄢", "涂", "钦", "归", "海","岳", "帅", "缑", "亢", "况", "后", "有", "琴", "商", "牟", "佘", "佴", "伯", "赏", "墨", "哈", "谯", "笪", "年", "爱", "阳", "佟", "言", "福" ;
std::string NA2[59] = "万俟", "司马", "上官", "欧阳", "夏侯", "诸葛", "闻人", "东方", "赫连", "皇甫", "尉迟", "公羊", "澹台", "公冶", "宗政", "濮阳", "淳于", "单于", "太叔", "申屠", "公孙", "仲孙", "轩辕", "令狐", "钟离", "宇文", "长孙", "慕容", "鲜于", "闾丘", "司徒", "司空", "亓官", "司寇", "子车", "颛孙", "端木", "巫马", "公西", "漆雕", "乐正", "壤驷", "公良", "拓跋", "夹谷", "宰父", "谷梁", "百里", "东郭", "南门", "呼延", "羊舌", "微生", "梁丘", "左丘", "东门", "西门", "南宫", "第五" ;
std::string ME1m[140] = "伟", "刚", "勇", "毅", "俊", "峰", "强", "军", "平", "保", "东", "文", "辉", "力", "明", "永", "健", "世", "广", "志", "义", "兴", "良", "海", "山", "仁", "波", "宁", "贵", "福", "生", "龙", "元", "全", "国", "胜", "学", "祥", "才", "发", "武", "新", "利", "清", "飞", "彬", "富", "顺", "信", "子", "杰", "涛", "昌", "成", "康", "星", "光", "天", "达", "安", "岩", "中", "茂", "进", "林", "有", "坚", "和", "彪", "博", "诚", "先", "敬", "震", "振", "壮", "会", "思", "群", "豪", "心", "邦", "承", "乐", "绍", "功", "松", "善", "厚", "庆", "磊", "民", "友", "裕", "河", "哲", "江", "超", "浩", "亮", "政", "谦", "亨", "奇", "固", "之", "轮", "翰", "朗", "伯", "宏", "言", "若", "鸣", "朋", "斌", "梁", "栋", "维", "启", "克", "伦", "翔", "旭", "鹏", "泽", "晨", "辰", "士", "以", "建", "家", "致", "树", "炎", "德", "行", "时", "泰", "盛" ;
std::string ME1f[165] = "秀", "娟", "英", "华", "慧", "巧", "美", "娜", "静", "淑", "惠", "珠", "翠", "雅", "芝", "玉", "萍", "红", "娥", "玲", "芬", "芳", "燕", "彩", "春", "菊", "兰", "凤", "洁", "梅", "琳", "素", "云", "莲", "真", "环", "雪", "荣", "爱", "妹", "霞", "香", "月", "莺", "媛", "艳", "瑞", "凡", "佳", "嘉", "琼", "勤", "珍", "贞", "莉", "桂", "娣", "叶", "璧", "璐", "娅", "琦", "晶", "妍", "茜", "秋", "珊", "莎", "锦", "黛", "青", "倩", "婷", "姣", "婉", "娴", "瑾", "颖", "露", "瑶", "怡", "婵", "雁", "蓓", "纨", "仪", "荷", "丹", "蓉", "眉", "君", "琴", "蕊", "薇", "菁", "梦", "岚", "苑", "筠", "柔", "竹", "霭", "凝", "晓", "欢", "霄", "枫", "芸", "菲", "寒", "欣", "滢", "伊", "亚", "宜", "可", "姬", "舒", "影", "荔", "枝", "思", "丽", "秀", "飘", "育", "馥", "琦", "晶", "妍", "茜", "秋", "珊", "莎", "锦", "黛", "青", "倩", "婷", "宁","蓓", "纨", "苑", "婕", "馨", "瑗", "琰", "韵", "融", "园", "艺", "咏", "卿", "聪", "澜", "纯", "毓", "悦", "昭", "冰", "爽", "琬", "茗", "羽", "希" ;
std::string sex = (rand()%2 == 0?"男":"女");//男女选择.
name = (rand()%56 != 5?NA1[rand()%444]:NA2[rand()%59]);//单姓Or复姓选择.
name += (sex == "男" ? ME1m[rand()%140] : ME1f[rand()%165]);//取名第一字.
if(rand()%2 == 0) name += (sex == "男" ? ME1m[rand()%140] : ME1f[rand()%165]);//取名第二字.
int main(int argc, char *argv[])
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QHListModel listModel;
for(int i=0;i<1000;i++)
std::string name;
DataBaseRoll(name);
QString userName = QString::fromStdString(name);
listModel.addData(i,userName);
QQmlApplicationEngine engine;
QQmlContext *context = engine.rootContext();
context->setContextProperty("searchModel", &listModel);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
main函数里没啥特别的,随机生成了1000个名字并添加到自定义model中
运行效果如下:
但是像这样一种数据对应一个roleName还是比较麻烦的,实际上我们可以只使用一个roleName(比如说modelData),然后roleName对应的数据是一个容器(通常是指针类型),容器中包含着所需的各种属性(userID、userName、systemType、loginTime、online等等)
此时如果要用SortFilterProxyModel过滤userName,像上面那样写就不行的,实际上应该像下面这样写
SortFilterProxyModel
id: proxyModel
sourceModel: searchModel
filters: RegExpFilter
roleName: "modelData.userName"
pattern: textField.text
caseSensitivity: Qt.CaseInsensitive
但是SortFilterProxyModel对"modelData.userName"这种二级roleName解析会有问题,无法实现想要的效果,因此得修改其源码才行
原文链接:https://blog.csdn.net/caoshangpa/article/details/126446067
以上是关于Qt优秀开源项目之十四:SortFilterProxyModel的主要内容,如果未能解决你的问题,请参考以下文章