├── QListViewDemo.pro
├── QListViewDemo.pro.user
├── README.md
├── itemdef.h
├── itemdelegate.cpp
├── itemdelegate.h
├── main.cpp
├── mainwindow.cpp
├── mainwindow.h
└── mainwindow.ui
/QListViewDemo.pro:
--------------------------------------------------------------------------------
1 | #-------------------------------------------------
2 | #
3 | # Project created by QtCreator 2017-07-29T16:30:00
4 | #
5 | #-------------------------------------------------
6 |
7 | QT += core gui
8 |
9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10 |
11 | TARGET = QListViewDemo
12 | TEMPLATE = app
13 |
14 | # The following define makes your compiler emit warnings if you use
15 | # any feature of Qt which as been marked as deprecated (the exact warnings
16 | # depend on your compiler). Please consult the documentation of the
17 | # deprecated API in order to know how to port your code away from it.
18 | DEFINES += QT_DEPRECATED_WARNINGS
19 |
20 | # You can also make your code fail to compile if you use deprecated APIs.
21 | # In order to do so, uncomment the following line.
22 | # You can also select to disable deprecated APIs only up to a certain version of Qt.
23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
24 |
25 |
26 | SOURCES += main.cpp\
27 | mainwindow.cpp \
28 | itemdelegate.cpp
29 |
30 | HEADERS += mainwindow.h \
31 | itemdelegate.h \
32 | itemdef.h
33 |
34 | FORMS += mainwindow.ui
35 |
--------------------------------------------------------------------------------
/QListViewDemo.pro.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | EnvironmentId
7 | {fa6b55a0-ad3c-42b4-b187-72760eef9ba0}
8 |
9 |
10 | ProjectExplorer.Project.ActiveTarget
11 | 0
12 |
13 |
14 | ProjectExplorer.Project.EditorSettings
15 |
16 | true
17 | false
18 | true
19 |
20 | Cpp
21 |
22 | CppGlobal
23 |
24 |
25 |
26 | QmlJS
27 |
28 | QmlJSGlobal
29 |
30 |
31 | 2
32 | UTF-8
33 | false
34 | 4
35 | false
36 | 80
37 | true
38 | true
39 | 1
40 | true
41 | false
42 | 0
43 | true
44 | true
45 | 0
46 | 8
47 | true
48 | 1
49 | true
50 | true
51 | true
52 | false
53 |
54 |
55 |
56 | ProjectExplorer.Project.PluginSettings
57 |
58 |
59 |
60 | ProjectExplorer.Project.Target.0
61 |
62 | Desktop Qt 5.7.1 MinGW 32bit
63 | Desktop Qt 5.7.1 MinGW 32bit
64 | qt.57.win32_mingw53_kit
65 | 0
66 | 0
67 | 0
68 |
69 | E:/QtProject/build-QListViewDemo-Desktop_Qt_5_7_1_MinGW_32bit-Debug
70 |
71 |
72 | true
73 | qmake
74 |
75 | QtProjectManager.QMakeBuildStep
76 | true
77 |
78 | false
79 | false
80 | false
81 |
82 |
83 | true
84 | Make
85 |
86 | Qt4ProjectManager.MakeStep
87 |
88 | false
89 |
90 |
91 |
92 | 2
93 | 构建
94 |
95 | ProjectExplorer.BuildSteps.Build
96 |
97 |
98 |
99 | true
100 | Make
101 |
102 | Qt4ProjectManager.MakeStep
103 |
104 | true
105 | clean
106 |
107 |
108 | 1
109 | 清理
110 |
111 | ProjectExplorer.BuildSteps.Clean
112 |
113 | 2
114 | false
115 |
116 | Debug
117 |
118 | Qt4ProjectManager.Qt4BuildConfiguration
119 | 2
120 | true
121 |
122 |
123 | E:/QtProject/build-QListViewDemo-Desktop_Qt_5_7_1_MinGW_32bit-Release
124 |
125 |
126 | true
127 | qmake
128 |
129 | QtProjectManager.QMakeBuildStep
130 | false
131 |
132 | false
133 | false
134 | false
135 |
136 |
137 | true
138 | Make
139 |
140 | Qt4ProjectManager.MakeStep
141 |
142 | false
143 |
144 |
145 |
146 | 2
147 | 构建
148 |
149 | ProjectExplorer.BuildSteps.Build
150 |
151 |
152 |
153 | true
154 | Make
155 |
156 | Qt4ProjectManager.MakeStep
157 |
158 | true
159 | clean
160 |
161 |
162 | 1
163 | 清理
164 |
165 | ProjectExplorer.BuildSteps.Clean
166 |
167 | 2
168 | false
169 |
170 | Release
171 |
172 | Qt4ProjectManager.Qt4BuildConfiguration
173 | 0
174 | true
175 |
176 |
177 | E:/QtProject/build-QListViewDemo-Desktop_Qt_5_7_1_MinGW_32bit-Profile
178 |
179 |
180 | true
181 | qmake
182 |
183 | QtProjectManager.QMakeBuildStep
184 | true
185 |
186 | false
187 | true
188 | false
189 |
190 |
191 | true
192 | Make
193 |
194 | Qt4ProjectManager.MakeStep
195 |
196 | false
197 |
198 |
199 |
200 | 2
201 | 构建
202 |
203 | ProjectExplorer.BuildSteps.Build
204 |
205 |
206 |
207 | true
208 | Make
209 |
210 | Qt4ProjectManager.MakeStep
211 |
212 | true
213 | clean
214 |
215 |
216 | 1
217 | 清理
218 |
219 | ProjectExplorer.BuildSteps.Clean
220 |
221 | 2
222 | false
223 |
224 | Profile
225 |
226 | Qt4ProjectManager.Qt4BuildConfiguration
227 | 0
228 | true
229 |
230 | 3
231 |
232 |
233 | 0
234 | 部署
235 |
236 | ProjectExplorer.BuildSteps.Deploy
237 |
238 | 1
239 | 在本地部署
240 |
241 | ProjectExplorer.DefaultDeployConfiguration
242 |
243 | 1
244 |
245 |
246 | false
247 | false
248 | 1000
249 |
250 | true
251 |
252 | false
253 | false
254 | false
255 | false
256 | true
257 | 0.01
258 | 10
259 | true
260 | 1
261 | 25
262 |
263 | 1
264 | true
265 | false
266 | true
267 | valgrind
268 |
269 | 0
270 | 1
271 | 2
272 | 3
273 | 4
274 | 5
275 | 6
276 | 7
277 | 8
278 | 9
279 | 10
280 | 11
281 | 12
282 | 13
283 | 14
284 |
285 | 2
286 |
287 | QListViewDemo
288 |
289 | Qt4ProjectManager.Qt4RunConfiguration:E:/QtProject/QListViewDemo/QListViewDemo.pro
290 | true
291 |
292 | QListViewDemo.pro
293 | false
294 |
295 |
296 | 3768
297 | false
298 | true
299 | false
300 | false
301 | true
302 |
303 | 1
304 |
305 |
306 |
307 | ProjectExplorer.Project.TargetCount
308 | 1
309 |
310 |
311 | ProjectExplorer.Project.Updater.FileVersion
312 | 18
313 |
314 |
315 | Version
316 | 18
317 |
318 |
319 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # QListView使用Demo
2 |
3 | ## 运行环境 :Qt5.7 win10
4 |
5 | ## 实现功能:ListView的展示、过滤,model数据的修改
--------------------------------------------------------------------------------
/itemdef.h:
--------------------------------------------------------------------------------
1 | #ifndef ITEMDEF_H
2 | #define ITEMDEF_H
3 |
4 | #include
5 |
6 | typedef enum{
7 | S_RED,
8 | S_BLUE,
9 | S_YELLOW,
10 | } ItemStatus;
11 |
12 | struct ItemData{
13 | QString name;
14 | QString tel;
15 | };
16 |
17 | Q_DECLARE_METATYPE(ItemData)
18 |
19 | #endif // ITEMDEF_H
20 |
--------------------------------------------------------------------------------
/itemdelegate.cpp:
--------------------------------------------------------------------------------
1 | #include "itemdelegate.h"
2 |
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include "itemdef.h"
9 |
10 | ItemDelegate::ItemDelegate(QObject *parent) :
11 | QStyledItemDelegate(parent)
12 | {
13 |
14 | }
15 |
16 | ItemDelegate::~ItemDelegate()
17 | {
18 |
19 | }
20 |
21 | void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
22 | {
23 |
24 |
25 | if(index.isValid())
26 | {
27 | painter->save();
28 |
29 | ItemStatus status = (ItemStatus)(index.data(Qt::UserRole).toInt());
30 |
31 | QVariant variant = index.data(Qt::UserRole+1);
32 | ItemData data = variant.value();
33 |
34 | QStyleOptionViewItem viewOption(option);//用来在视图中画一个item
35 |
36 | QRectF rect;
37 | rect.setX(option.rect.x());
38 | rect.setY(option.rect.y());
39 | rect.setWidth( option.rect.width()-1);
40 | rect.setHeight(option.rect.height()-1);
41 |
42 | //QPainterPath画圆角矩形
43 | const qreal radius = 7;
44 | QPainterPath path;
45 | path.moveTo(rect.topRight() - QPointF(radius, 0));
46 | path.lineTo(rect.topLeft() + QPointF(radius, 0));
47 | path.quadTo(rect.topLeft(), rect.topLeft() + QPointF(0, radius));
48 | path.lineTo(rect.bottomLeft() + QPointF(0, -radius));
49 | path.quadTo(rect.bottomLeft(), rect.bottomLeft() + QPointF(radius, 0));
50 | path.lineTo(rect.bottomRight() - QPointF(radius, 0));
51 | path.quadTo(rect.bottomRight(), rect.bottomRight() + QPointF(0, -radius));
52 | path.lineTo(rect.topRight() + QPointF(0, radius));
53 | path.quadTo(rect.topRight(), rect.topRight() + QPointF(-radius, -0));
54 |
55 | if(option.state.testFlag(QStyle::State_Selected))
56 | {
57 | painter->setPen(QPen(Qt::blue));
58 | painter->setBrush(QColor(229, 241, 255));
59 | painter->drawPath(path);
60 | }
61 | else if(option.state.testFlag(QStyle::State_MouseOver))
62 | {
63 | painter->setPen(QPen(Qt::green));
64 | painter->setBrush(Qt::NoBrush);
65 | painter->drawPath(path);
66 | }
67 | else{
68 | painter->setPen(QPen(Qt::gray));
69 | painter->setBrush(Qt::NoBrush);
70 | painter->drawPath(path);
71 | }
72 |
73 | //绘制数据位置
74 | QRect NameRect = QRect(rect.left() +10, rect.top()+10, rect.width()-30, 20);
75 | QRect circle = QRect(NameRect.right(), rect.top()+10, 10, 10);
76 | QRect telRect = QRect(rect.left() +10, rect.bottom()-25, rect.width()-10, 20);
77 |
78 |
79 | switch (status) {
80 | case S_RED:
81 | painter->setBrush(Qt::red);
82 | painter->setPen(QPen(Qt::red));
83 | break;
84 | case S_BLUE:
85 | painter->setBrush(Qt::blue);
86 | painter->setPen(QPen(Qt::blue));
87 | break;
88 | case S_YELLOW:
89 | painter->setBrush(Qt::yellow);
90 | painter->setPen(QPen(Qt::yellow));
91 | break;
92 | }
93 |
94 | painter->drawEllipse(circle); //画圆圈
95 |
96 | painter->setPen(QPen(Qt::black));
97 | painter->setFont(QFont("Times", 12, QFont::Bold));
98 | painter->drawText(NameRect,Qt::AlignLeft,data.name); //绘制名字
99 |
100 | painter->setPen(QPen(Qt::gray));
101 | painter->setFont(QFont("Times", 10));
102 | painter->drawText(telRect,Qt::AlignLeft,data.tel); //绘制电话
103 |
104 | painter->restore();
105 | }
106 | }
107 |
108 | QSize ItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
109 | {
110 | return QSize(160, 60);
111 | }
112 |
--------------------------------------------------------------------------------
/itemdelegate.h:
--------------------------------------------------------------------------------
1 | #ifndef ITEMDELEGATE_H
2 | #define ITEMDELEGATE_H
3 | #include
4 | #include
5 | #include
6 |
7 | class ItemDelegate : public QStyledItemDelegate
8 | {
9 | Q_OBJECT
10 | signals:
11 |
12 | public:
13 | explicit ItemDelegate(QObject *parent = 0);
14 | ~ItemDelegate();
15 |
16 | //重写重画函数
17 | void paint(QPainter * painter,const QStyleOptionViewItem & option,const QModelIndex & index) const;
18 | QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
19 |
20 | private:
21 |
22 | };
23 |
24 | #endif // ITEMDELEGATE_H
25 |
--------------------------------------------------------------------------------
/main.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include
3 |
4 | int main(int argc, char *argv[])
5 | {
6 | QApplication a(argc, argv);
7 | MainWindow w;
8 | w.show();
9 |
10 | return a.exec();
11 | }
12 |
--------------------------------------------------------------------------------
/mainwindow.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include "ui_mainwindow.h"
3 | #include "itemdelegate.h"
4 | #include "itemdef.h"
5 | #include
6 |
7 | MainWindow::MainWindow(QWidget *parent) :
8 | QMainWindow(parent),
9 | ui(new Ui::MainWindow)
10 | {
11 | ui->setupUi(this);
12 |
13 | initData();
14 | updateButtonNum();
15 |
16 | m_delegate = new ItemDelegate(this);
17 | m_filterButtonGroup = new QButtonGroup(this);
18 |
19 | // 设置互斥
20 | m_filterButtonGroup->setExclusive(true);
21 |
22 | m_filterButtonGroup->addButton(ui->allBtn);
23 | m_filterButtonGroup->addButton(ui->redBtn);
24 | m_filterButtonGroup->addButton(ui->blueBtn);
25 | m_filterButtonGroup->addButton(ui->yellowBtn);
26 |
27 | // 连接信号槽
28 | connect(m_filterButtonGroup, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(onButtonClicked(QAbstractButton*)));
29 |
30 | ui->listView->setItemDelegate(m_delegate); //为视图设置委托
31 | ui->listView->setSpacing(15); //为视图设置控件间距
32 | m_proxyModel = new QSortFilterProxyModel(ui->listView);
33 | m_proxyModel->setSourceModel(m_model);
34 | m_proxyModel->setFilterRole(Qt::UserRole);
35 | m_proxyModel->setDynamicSortFilter(true);
36 | ui->listView->setModel(m_proxyModel); //为委托设置模型
37 | ui->listView->setViewMode(QListView::IconMode); //设置Item图标显示
38 | ui->listView->setDragEnabled(false); //控件不允许拖动
39 | }
40 |
41 | MainWindow::~MainWindow()
42 | {
43 | delete ui;
44 | }
45 |
46 | void MainWindow::initData()
47 | {
48 | totalNum = 50;
49 | redNum = 0;
50 | blueNum = 0;
51 | yellowNum = 0;
52 |
53 | m_model = new QStandardItemModel();
54 |
55 | for (int i = 0; i < totalNum; ++i) {
56 | QStandardItem *Item = new QStandardItem;
57 |
58 | ItemData itemData;
59 |
60 | itemData.name = QString("Name %1").arg(i);
61 | itemData.tel = QString("TEL:1331234567%1").arg(i);
62 | int randNum = rand()% 3;
63 | ItemStatus itemStatus;
64 | switch (randNum) {
65 | case 0:
66 | itemStatus = S_RED;
67 | redNum++;
68 | break;
69 | case 1:
70 | itemStatus = S_BLUE;
71 | blueNum++;
72 | break;
73 | case 2:
74 | itemStatus = S_YELLOW;
75 | yellowNum++;
76 | break;
77 | }
78 | Item->setData(itemStatus,Qt::UserRole); // 单一存取
79 | Item->setData(QVariant::fromValue(itemData),Qt::UserRole+1);//整体存取
80 |
81 | m_model->appendRow(Item); //追加Item
82 | }
83 | }
84 |
85 | void MainWindow::onButtonClicked(QAbstractButton *button)
86 | {// 当前点击的按钮
87 |
88 | qDebug() << button->text();
89 |
90 | }
91 |
92 | void MainWindow::updateButtonNum()
93 | {
94 | ui->allBtn->setText(tr("all %1").arg(totalNum));
95 | ui->redBtn->setText(tr("red %1").arg(redNum));
96 | ui->blueBtn->setText(tr("blue %1").arg(blueNum));
97 | ui->yellowBtn->setText(tr("yellow %1").arg(yellowNum));
98 | }
99 |
100 | void MainWindow::on_redBtn_clicked()
101 | {
102 | if(m_proxyModel)
103 | {
104 | m_proxyModel->setFilterFixedString(QString::number(S_RED));
105 | }
106 | }
107 |
108 | void MainWindow::on_allBtn_clicked()
109 | {
110 | if(m_proxyModel)
111 | {
112 | m_proxyModel->setFilterFixedString(QString());
113 | }
114 | }
115 |
116 | void MainWindow::on_blueBtn_clicked()
117 | {
118 | if(m_proxyModel)
119 | {
120 | m_proxyModel->setFilterFixedString(QString::number(S_BLUE));
121 | }
122 | }
123 |
124 | void MainWindow::on_yellowBtn_clicked()
125 | {
126 | if(m_proxyModel)
127 | {
128 | m_proxyModel->setFilterFixedString(QString::number(S_YELLOW));
129 | }
130 | }
131 |
132 | void MainWindow::on_setRedBtn_clicked()
133 | {
134 | QModelIndexList modelIndexList = ui->listView->selectionModel()->selectedIndexes();
135 | QModelIndexList sourceIndexList;
136 | foreach (QModelIndex modelIndex, modelIndexList){
137 | sourceIndexList<mapToSource(modelIndex); //获取源model的modelIndex
138 | }
139 |
140 | // g_proxyModel->setDynamicSortFilter(false);
141 | foreach (QModelIndex sourceIndex, sourceIndexList){
142 | ItemStatus status = (ItemStatus)(sourceIndex.data(Qt::UserRole).toInt());
143 | qDebug() << "Index : " << sourceIndex.row();
144 |
145 | switch (status) {
146 | case S_RED:
147 | redNum--;
148 | break;
149 | case S_BLUE:
150 | blueNum--;
151 | break;
152 | case S_YELLOW:
153 | yellowNum--;
154 | break;
155 | }
156 |
157 | status = S_RED;
158 | redNum++;
159 |
160 | m_model->setData(sourceIndex,status,Qt::UserRole);
161 | }
162 | // g_proxyModel->setDynamicSortFilter(true);
163 | updateButtonNum();
164 | }
165 |
--------------------------------------------------------------------------------
/mainwindow.h:
--------------------------------------------------------------------------------
1 | #ifndef MAINWINDOW_H
2 | #define MAINWINDOW_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include "itemdelegate.h"
9 |
10 | namespace Ui {
11 | class MainWindow;
12 | }
13 |
14 | class MainWindow : public QMainWindow
15 | {
16 | Q_OBJECT
17 |
18 | public:
19 | explicit MainWindow(QWidget *parent = 0);
20 | ~MainWindow();
21 |
22 | private slots:
23 | void onButtonClicked(QAbstractButton *button);
24 | void on_redBtn_clicked();
25 |
26 | void on_allBtn_clicked();
27 |
28 | void on_blueBtn_clicked();
29 |
30 | void on_yellowBtn_clicked();
31 |
32 | void on_setRedBtn_clicked();
33 |
34 | private:
35 | Ui::MainWindow *ui;
36 | QButtonGroup *m_filterButtonGroup;
37 | ItemDelegate *m_delegate; //委托
38 | QSortFilterProxyModel* m_proxyModel;
39 | QStandardItemModel *m_model;
40 | int totalNum;
41 | int redNum;
42 | int blueNum;
43 | int yellowNum;
44 |
45 | void initData();
46 | void updateButtonNum();
47 | };
48 |
49 | #endif // MAINWINDOW_H
50 |
--------------------------------------------------------------------------------
/mainwindow.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 771
10 | 536
11 |
12 |
13 |
14 | MainWindow
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | QWidget#centralWidget{
23 | background-color: white;
24 | }
25 |
26 |
27 |
28 |
29 | 10
30 | 90
31 | 751
32 | 401
33 |
34 |
35 |
36 | QListView {
37 | background-color: transparent;
38 | border:1px solid #EFEFEF;
39 | }
40 |
41 |
42 |
43 |
44 | Qt::ScrollBarAsNeeded
45 |
46 |
47 | Qt::ScrollBarAsNeeded
48 |
49 |
50 | QAbstractItemView::NoEditTriggers
51 |
52 |
53 | QAbstractItemView::MultiSelection
54 |
55 |
56 |
57 |
58 |
59 | 120
60 | 30
61 | 100
62 | 23
63 |
64 |
65 |
66 |
67 | 12
68 |
69 |
70 |
71 | QPushButton#redBtn{
72 | background:white;
73 | border:1px solid rgb(255, 84, 92);
74 | border-radius:5px;
75 | padding:2px 4px;
76 | color:rgb(255, 84, 92);
77 | }
78 |
79 | QPushButton#redBtn:checked{
80 | background:rgb(255, 84, 92);
81 | color:white;
82 | }
83 |
84 |
85 | red
86 |
87 |
88 | true
89 |
90 |
91 | false
92 |
93 |
94 |
95 |
96 |
97 | 230
98 | 30
99 | 100
100 | 23
101 |
102 |
103 |
104 |
105 | 12
106 |
107 |
108 |
109 | QPushButton#blueBtn{
110 | background:white;
111 | border:1px solid #3288E8;
112 | border-radius:5px;
113 | padding:2px 4px;
114 | color:#3288E8;
115 | }
116 |
117 | QPushButton#blueBtn:checked{
118 | background:#3288E8;
119 | color:white;
120 | }
121 |
122 |
123 | blue
124 |
125 |
126 | true
127 |
128 |
129 |
130 |
131 |
132 | 340
133 | 30
134 | 100
135 | 23
136 |
137 |
138 |
139 |
140 | 12
141 |
142 |
143 |
144 | QPushButton#yellowBtn{
145 | background:white;
146 | border:1px solid rgb(255, 226, 6);
147 | border-radius:5px;
148 | padding:2px 4px;
149 | color:rgb(255, 226, 6);
150 | }
151 |
152 | QPushButton#yellowBtn:checked{
153 | background:rgb(255, 226, 6);
154 | color:white;
155 | }
156 |
157 |
158 | yellow
159 |
160 |
161 | true
162 |
163 |
164 |
165 |
166 |
167 | 10
168 | 30
169 | 100
170 | 23
171 |
172 |
173 |
174 |
175 | 12
176 |
177 |
178 |
179 | QPushButton#allBtn{
180 | background:white;
181 | border:1px solid gray;
182 | border-radius:5px;
183 | padding:2px 4px;
184 | color:gray;
185 | }
186 |
187 | QPushButton#allBtn:checked{
188 | background:gray;
189 | color:white;
190 | }
191 |
192 |
193 | all
194 |
195 |
196 | true
197 |
198 |
199 | true
200 |
201 |
202 |
203 |
204 |
205 | 630
206 | 30
207 | 75
208 | 23
209 |
210 |
211 |
212 | QPushButton#setRedBtn{
213 | border:1px solid rgb(255, 84, 92);
214 | border-radius:5px;
215 | padding:2px 4px;
216 | background:rgb(255, 84, 92);
217 | color:white;
218 | }
219 |
220 |
221 | set red
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
--------------------------------------------------------------------------------