QTの中のView Model模型のシリーズの1

7236 ワード

今回は主にQTのView Modelの基本構造と簡単な使い方を紹介します.View Modelの概念についてはここで説明します.簡単な理解は参考できます.
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
以下は本文に入ります.
QtのViewは主に三つのQListView、QTree View、QTabel Viewがあります.
対応するモデルは、QStrigListModelであり、 QbstractItemModel , QStardardItemModel
 
基本的な原理はViewを作り、ModelとViewのset Modelを通して両者を結合することです.
以下は各種類のView-moodelの使用を紹介します.
 QListViewとQStrigListModel
  Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->#include 
#include 
#include 
#include 
#include 
 
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();
}
QTablew を選択します QStandardItemModel
  Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->#include 
#include 
#include 
#include 
 
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
 
QStandardItemModel *model = new QStandardItemModel;
model->setItem(0, 0, new QStandardItem("January"));
model->setItem(1, 0, new QStandardItem("February"));
model->setItem(0, 1, new QStandardItem("10,000"));
model->setItem(1, 1, new 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には一般的にQbstractItem Modelを積載して自分のModelを実現する必要があります.次の説明で紹介します.
 
簡単な応用についてはQListWidget,QTree Widget,QTabel Widgetも提供している. 三つのwidgetはそれぞれ上の3つのViewから継承されています.それらはView、Modelを統合して、使いやすいです.
サンプルプログラム: 
 QListWidget
  Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->#include 
#include 
#include 
#include 
 
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(0, new QListWidgetItem(
QIcon("caterpillar_head.jpg"), "caterpillar"));
listWidget->insertItem(1, new QListWidgetItem(
QIcon("momor_head.jpg"), "momor"));
listWidget->insertItem(2, new QListWidgetItem(
QIcon("bush_head.jpg"), "bush"));
listWidget->insertItem(3, new QListWidgetItem(
QIcon("bee_head.jpg"), "bee"));
listWidget->insertItem(4, new 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();
}
QTree Widget
  Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
void listFile(QTreeWidgetItem *, QFileInfo &);
 
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
 
QTreeWidget *treeWidget = new QTreeWidget;
treeWidget->setWindowTitle("QTreeWidget & Item");
treeWidget->resize(400, 250);
 
//        
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);
}
}    
}
QTable Widget 
  Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->#include 
#include 
#include 
 
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
 
QTableWidget *tableWidget = new QTableWidget;
tableWidget->setWindowTitle("QTableWidget & Item");
tableWidget->resize(350, 200);
tableWidget->setRowCount(10);
tableWidget->setColumnCount(5);
 
QStringList header;
header.append("Month");
header.append("Description");
tableWidget->setHorizontalHeaderLabels(header);
 
tableWidget->setItem(0, 0, new QTableWidgetItem("January"));
tableWidget->setItem(1, 0, new QTableWidgetItem("February"));
tableWidget->setItem(2, 0, new QTableWidgetItem("March"));
 
tableWidget->setItem(0, 1, 
new QTableWidgetItem(QIcon("caterpillar_head.jpg"), "caterpillar's month"));
tableWidget->setItem(1, 1, 
new QTableWidgetItem(QIcon("momor_head.jpg"), "momor's month"));      
tableWidget->setItem(2, 1, 
new QTableWidgetItem(QIcon("bush_head.jpg"), "bush's month"));        
 
tableWidget->show();
 
return app.exec();
}