一、Model/View框架简介
1、Model/View框架核心思想
2、Model/View框架工作机制
3、Model/View框架的类
二、Model
1、模型简介
2、模型索引
3、数据角色
4、QStringListModel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include <QApplication>
#include <QStringListModel>
#include <QListView>
#include <QStringList>
int
main(
int
argc,
char
*argv[])
{
QApplication a(argc, argv);
QStringListModel* listModel =
new
QStringListModel;
QListView* view =
new
QListView;
QStringList nameList;
nameList<<
"ZheDong Mao"
<<
"RenLai Zhou"
<<
"XiaoPing Deng"
<<
"ShaoQi Liu"
<<
"De Zhu"
<<
"DeHuai Peng"
;
listModel->setStringList(nameList);
view->setModel(listModel);
view->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView::DoubleClicked);
view->setWindowTitle(
"QStringListModel"
);
view->show();
return
a.exec();
}
|
5、QFileSystemModel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#include <QApplication>
#include <QFileSystemModel>
#include <QTreeView>
#include <QListView>
int
main(
int
argc,
char
* argv[])
{
QApplication app(argc,argv);
//创建文件系统模型
QFileSystemModel model;
//指定根目录
model.setRootPath(QDir::currentPath());
//创建树形视图
QTreeView tree;
//为视图指定模型
tree.setModel(&model);
//指定根索引
tree.setRootIndex(model.index(QDir::currentPath()));
//创建列表视图
QListView list;
list.setModel(&model);
list.setRootIndex(model.index(QDir::currentPath()));
tree.show();
list.show();
return
app.exec();
}
|
6、QSortFilterProxyModel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#include <QApplication>
#include <QFileSystemModel>
#include <QSortFilterProxyModel>
#include <QTreeView>
#include <QListView>
int
main(
int
argc,
char
* argv[])
{
QApplication app(argc,argv);
//创建文件系统模型
QFileSystemModel model;
//指定根目录
model.setRootPath(QDir::currentPath());
//创建树形视图
QSortFilterProxyModel proxymodel;
proxymodel.setFilterKeyColumn(0);
proxymodel.setSourceModel(&model);
QTreeView tree;
//为视图指定模型
tree.setModel(&proxymodel);
//指定根索引
tree.setRootIndex(proxymodel.index(0, 0, QModelIndex()));
//创建列表视图
QListView list;
list.setModel(&proxymodel);
list.setRootIndex(proxymodel.index(0, 0, QModelIndex()));
tree.show();
list.show();
return
app.exec();
}
|