惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
博客园 - 叶小钗
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
TaoSecurity Blog
TaoSecurity Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
爱范儿
爱范儿
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
量子位
N
News and Events Feed by Topic
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Commits to openclaw:main
Recent Commits to openclaw:main
SecWiki News
SecWiki News
MyScale Blog
MyScale Blog
AI
AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 【当耐特】
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
有赞技术团队
有赞技术团队
W
WeLiveSecurity
Project Zero
Project Zero
T
Tor Project blog
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
IT之家
IT之家
The Hacker News
The Hacker News
腾讯CDC
Schneier on Security
Schneier on Security
N
News and Events Feed by Topic
C
Cisco Blogs
博客园 - 聂微东
Webroot Blog
Webroot Blog
Forbes - Security
Forbes - Security
M
MIT News - Artificial intelligence
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
About on SuperTechFans

博客园 - chinese_submarine

QT项目性能调优小记 Windows下HG服务器的搭建 svn+tp-link+花生壳搭建外网服务器 udev介绍 极大极小博弈树的简洁(附Tic-Tac-Toe源码) 程序优化小记 Beating the Average------为什么要学习Lisp[转] 巧用qmake工具生成专业的makefile QT中拖拽的实现(附示例代码) 从QDataStream向QByteArray中写入数据时的注意点(QT) 如何保持GUI的响应流畅(QT平台) 也谈线程同步变量 windows7到期的问题 简述FPS的计算方法 从农夫养牛问题推广到斐波那契数列 TimeZoneChange事件的捕获 浏览器扩展系列————透明浏览器窗口的实现 浏览器扩展系列————异步可插入协议(pluggable protocol)的实现 浏览器扩展系列————给MSTHML添加内置脚本对象【包括自定义事件】
QT中的View Model模型系列一
chinese_submarine · 2010-01-16 · via 博客园 - chinese_submarine

本讲主要介绍一下QT中ViewModel的基本结构,以及简单的使用方法。对于View Model的概念这里就不累述了,简单的了解可以参考:

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

下面进入正文:

Qt中的View主要有三种QListViewQTreeView, QTabelView

而对应的Model是:QStringListModel, QAbstractItemModel , QStandardItemModel

基本的原理就是构造出View,和Model然后通过ViewsetModel方法,将两者结合起来。

下面介绍每种View-Model的使用:

 QListViewQStringListModel

代码

#include <QApplication>
#include 
<QStringList>
#include 
<QAbstractItemModel>
#include 
<QStringListModel>
#include 
<QListView>
 
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
 
QStringList numbers;
numbers 
<< "caterpillar" << "momor" << "bush" << "bee";
QAbstractItemModel 
*model = new QStringListModel(numbers);
 
QListView 
*view = new QListView;
view
->setWindowTitle("QListView & Model");
view
->setModel(model);
 
view
->show();
 
return app.exec();
}

QTableView QStandardItemModel

代码

#include <QApplication>
#include 
<QStandardItemModel>
#include 
<QStandardItem>
#include 
<QTableView>
 
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
 
QStandardItemModel 
*model = new QStandardItemModel;
model
->setItem(00new QStandardItem("January"));
model
->setItem(10new QStandardItem("February"));
model
->setItem(01new QStandardItem("10,000"));
model
->setItem(11new QStandardItem("20,000"));
 
QTableView 
*view1 = new QTableView;
view1
->setModel(model);
 
QTableView 
*view2 = new QTableView;
view2
->setModel(model);
 
view1
->show();
view2
->show();
 
return app.exec();
}

对于QTreeView一般来说需要重载QAbstractItemModel来实现自己的Model,将在下一讲中介绍。

对于简单的应用,Qt还提供了QListWidget, QTreeWidget, QTabelWidget 三个widget,分别继承自上面的3View。它们将ViewModel整合了,可以方便的使用。

示例程序: 

 QListWidget

代码

#include <QApplication>
#include 
<QHBoxLayout>
#include 
<QLabel>
#include 
<QListWidget>
 
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
 
QWidget 
*window = new QWidget;
window
->setWindowTitle("QListWidget & Item");
 
QLabel 
*label = new QLabel;
label
->setFixedWidth (100);
 
QListWidget 
*listWidget = new QListWidget;
listWidget
->insertItem(0new QListWidgetItem(
QIcon(
"caterpillar_head.jpg"), "caterpillar"));
listWidget
->insertItem(1new QListWidgetItem(
QIcon(
"momor_head.jpg"), "momor"));
listWidget
->insertItem(2new QListWidgetItem(
QIcon(
"bush_head.jpg"), "bush"));
listWidget
->insertItem(3new QListWidgetItem(
QIcon(
"bee_head.jpg"), "bee"));
listWidget
->insertItem(4new QListWidgetItem(
QIcon(
"cat_head.jpg"), "cat"));
 
QObject::connect(listWidget, SIGNAL(currentTextChanged (
const QString &)),
label, SLOT(setText(
const QString &)));
 
QHBoxLayout 
*layout = new QHBoxLayout;
layout
->addWidget(label);
layout
->addWidget(listWidget);
 
window
->setLayout(layout); 
window
->show();
 
return app.exec();
}

QTreeWidget

代码

#include <QApplication>
#include 
<QTreeWidget>
#include 
<QTreeWidgetItem>
#include 
<QStringList>
#include 
<QFile>
#include 
<QFileInfo>
#include 
<QDir>
 
void listFile(QTreeWidgetItem *, QFileInfo &);
 
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
 
QTreeWidget 
*treeWidget = new QTreeWidget;
treeWidget
->setWindowTitle("QTreeWidget & Item");
treeWidget
->resize(400250);
 
// 設定欄位名稱 
QStringList columnTitle;
columnTitle.append(
"Name");
columnTitle.append(
"Size");
treeWidget
->setHeaderLabels(columnTitle);
 
// 查詢的目錄    
QFileInfo fileInfo("D:\\Temp");
QStringList fileColumn;
fileColumn.append(fileInfo.fileName());
 
QTreeWidgetItem 
*dir = new QTreeWidgetItem(fileColumn);
dir
->setIcon(0, QIcon("caterpillar_head.jpg"));
dir
->setCheckState(0, Qt::Checked); // 設定可核取的方塊 
treeWidget->addTopLevelItem(dir);
 
// 查詢目錄 
listFile(dir, fileInfo);
 
treeWidget
->show();
 
return app.exec();
}
 
void listFile(QTreeWidgetItem *parentWidgetItem, QFileInfo &parent) {
QDir dir;
dir.setPath(parent.filePath());
dir.setFilter(QDir::Files 
| QDir::Dirs | QDir::NoSymLinks);
dir.setSorting(QDir::DirsFirst 
| QDir::Name);    
 
const QFileInfoList fileList = dir.entryInfoList();
 
for (int i = 0; i < fileList.size(); i++) {
QFileInfo fileInfo 
= fileList.at(i);
QStringList fileColumn;
fileColumn.append(fileInfo.fileName());
if (fileInfo.fileName() == "." || fileInfo.fileName() == ".." ); // nothing
else if(fileInfo.isDir()) {
QTreeWidgetItem 
*child = new QTreeWidgetItem(fileColumn);
child
->setIcon(0, QIcon("caterpillar_head.jpg"));
child
->setCheckState(0, Qt::Checked);
parentWidgetItem
->addChild(child);
// 查詢子目錄
listFile(child, fileInfo);
}
else {
fileColumn.append(QString::number(fileInfo.size()));
QTreeWidgetItem 
*child = new QTreeWidgetItem(fileColumn);
child
->setIcon(0, QIcon("momor_head.jpg"));
child
->setCheckState(0, Qt::Checked);            
parentWidgetItem
->addChild(child);
}
}    
}

QTableWidget 

代码

#include <QApplication>
#include 
<QTableWidget>
#include 
<QHBoxLayout>
 
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
 
QTableWidget 
*tableWidget = new QTableWidget;
tableWidget
->setWindowTitle("QTableWidget & Item");
tableWidget
->resize(350200);
tableWidget
->setRowCount(10);
tableWidget
->setColumnCount(5);
 
QStringList header;
header.append(
"Month");
header.append(
"Description");
tableWidget
->setHorizontalHeaderLabels(header);
 
tableWidget
->setItem(00new QTableWidgetItem("January"));
tableWidget
->setItem(10new QTableWidgetItem("February"));
tableWidget
->setItem(20new QTableWidgetItem("March"));
 
tableWidget
->setItem(01
new QTableWidgetItem(QIcon("caterpillar_head.jpg"), "caterpillar's month"));
tableWidget
->setItem(11
new QTableWidgetItem(QIcon("momor_head.jpg"), "momor's month"));      
tableWidget
->setItem(21
new QTableWidgetItem(QIcon("bush_head.jpg"), "bush's month"));        
 
tableWidget
->show();
 
return app.exec();
}