南城區(qū)做網(wǎng)站中國(guó)十大小說網(wǎng)站排名
【嵌入式——QT】Model/View
- 基本原理
- 數(shù)據(jù)模型
- 視圖組件
- 代理
- Model/View結(jié)構(gòu)的一些概念
- QFileSystemModel
- QStringListModel
- QStandardItemModel
- 自定義代理
基本原理
GUI應(yīng)用程序的一個(gè)很重要的功能是由用戶在界面上編輯和修改數(shù)據(jù),典型的如數(shù)據(jù)庫(kù)應(yīng)用程序,數(shù)據(jù)庫(kù)應(yīng)用程序中,用戶在界面上執(zhí)行各種操作,實(shí)際上是修改了界面組件所關(guān)聯(lián)的數(shù)據(jù)庫(kù)內(nèi)的數(shù)據(jù)。將界面組件與所編輯的數(shù)據(jù)分離開來,又通過數(shù)據(jù)源的方式連接起來,是處理界面與數(shù)據(jù)的一種較好的方式,Qt使用Model/View結(jié)構(gòu)來處理這種關(guān)系。
數(shù)據(jù)模型
QAbstractItemModel
- QAbstractListModel:QStringListModel,處理字符串列表數(shù)據(jù)的數(shù)據(jù)模型類;
- QAbstractProxyModel:QSortFilterProxyModel,提供排序和過濾功能的數(shù)據(jù)模型類;
- QAbstractTableModel:QSqlQueryModel,數(shù)據(jù)庫(kù)SQL查詢結(jié)構(gòu)的數(shù)據(jù)模型類;QSqlTableModel,用于數(shù)據(jù)庫(kù)的一個(gè)數(shù)據(jù)表的數(shù)據(jù)模型類;QSqlRelationalTableModel,關(guān)系型數(shù)據(jù)表的數(shù)據(jù)模型類;
- QStandardItemModel:每個(gè)項(xiàng)數(shù)據(jù)可以使任何數(shù)據(jù)類型;
- QFileSystemModel:文件系統(tǒng)的數(shù)據(jù)模型類;
視圖組件
- QListView:用于顯示單列的列表數(shù)據(jù),適用于一維數(shù)據(jù)的操作;
- QTreeView:用于顯示樹狀結(jié)構(gòu)數(shù)據(jù),適用于樹狀結(jié)構(gòu)數(shù)據(jù)的操作;
- QTableView:用于顯示表格狀數(shù)據(jù),適用于二維表格型數(shù)據(jù)的操作;
- QColumnView:用多個(gè)QListView顯示樹狀層次結(jié)構(gòu),樹狀結(jié)構(gòu)的一層用一個(gè)QListView顯示;
- QHeaderView:提供行表頭或列表頭的視圖組件,如QTableView的行表頭和列表頭;
代理
代理(Delegate)就是在視圖組件上為編輯數(shù)據(jù)提供編輯器,如在表格組件中編輯一個(gè)單元格的數(shù)據(jù)時(shí),缺省是使用一個(gè)QLineEdit編輯框。
代理負(fù)責(zé)從數(shù)據(jù)模型獲取相應(yīng)的數(shù)據(jù),然后顯示在編輯器里,修改數(shù)據(jù)后,又將其保存到數(shù)據(jù)模型中。
QAbstractItemDelegate是所有代理類的基類,它不能直接使用,它的一個(gè)子類QStyledItemDelegate,是Qt的視圖組件缺省使用的代理類。
Model/View結(jié)構(gòu)的一些概念
基本結(jié)構(gòu)
數(shù)據(jù)模型中的存儲(chǔ)數(shù)據(jù)的基本單元都是項(xiàng)(item),每個(gè)項(xiàng)都有一個(gè)行號(hào),一個(gè)列號(hào),還有一個(gè)父項(xiàng)。
模型索引
QModelIndex表示模型索引的類,模型索引提供數(shù)據(jù)存取的一個(gè)臨時(shí)指針,用于通過數(shù)據(jù)模型提取或修改數(shù)據(jù),因?yàn)槟P蛢?nèi)部數(shù)據(jù)的結(jié)構(gòu)隨時(shí)可能改變,所以模型索引是臨時(shí)的。
行號(hào)和列號(hào)
數(shù)據(jù)模型的基本形式是用行和列定義的表格數(shù)據(jù),要獲得一個(gè)模型索引,必須提供行號(hào)、列號(hào)、父項(xiàng)的模型索引。
父項(xiàng)
當(dāng)數(shù)據(jù)模型是列表或表格時(shí),使用行號(hào)、列號(hào)存儲(chǔ)數(shù)據(jù)比較直觀,所有數(shù)據(jù)項(xiàng)的父項(xiàng)就是頂層項(xiàng),當(dāng)數(shù)據(jù)模型是樹狀結(jié)構(gòu)時(shí),情況比較復(fù)雜,一個(gè)節(jié)點(diǎn)可以有父節(jié)點(diǎn),也可以是其他節(jié)點(diǎn)的父節(jié)點(diǎn),在構(gòu)造數(shù)據(jù)項(xiàng)的模型索引時(shí),必須指定正確的行號(hào)、列號(hào)和父節(jié)點(diǎn)。
項(xiàng)的角色
在為數(shù)據(jù)模式的一個(gè)項(xiàng)設(shè)置數(shù)據(jù)時(shí),可以賦予其不同項(xiàng)的角色的數(shù)據(jù)。
enum ItemDataRole
- Qt::DisplayRole:在視圖組件中顯示字符串,標(biāo)準(zhǔn)角色;
- Qt::ToolTipRole:鼠標(biāo)提示消息;
- Qt::UserRole:可以自定義數(shù)據(jù);
QFileSystemModel
QFileSystemModel提供了一個(gè)可用于訪問本機(jī)文件系統(tǒng)的數(shù)據(jù)模型,QFileSystemModel和視圖組件QTreeView結(jié)合使用,可以用目錄樹的形式顯示本機(jī)上的文件系統(tǒng),如同Windows的資源管理器一樣,使用QFileSystemModel提供的接口函數(shù),可以創(chuàng)建目錄,刪除目錄,重命名目錄,可以獲得文件名稱、目錄名稱、文件大小等參數(shù),還可以獲得文件的詳細(xì)信息。
圖示
代碼示例
ModelViewDialog.h
#ifndef MODELVIEWDIALOG_H
#define MODELVIEWDIALOG_H#include <QDialog>
#include <QFileSystemModel>namespace Ui
{class ModelViewDialog;
}class ModelViewDialog : public QDialog
{Q_OBJECTpublic:explicit ModelViewDialog(QWidget* parent = nullptr);~ModelViewDialog();
private slots:void on_treeView_clicked(const QModelIndex& index);private:Ui::ModelViewDialog* ui;QFileSystemModel* model;
};#endif // MODELVIEWDIALOG_H
ModelViewDialog.cpp
#include "ModelViewDialog.h"
#include "ui_ModelViewDialog.h"
ModelViewDialog::ModelViewDialog(QWidget* parent): QDialog(parent), ui(new Ui::ModelViewDialog)
{ui->setupUi(this);model = new QFileSystemModel(this);//QDir::currentPath() 獲得本機(jī)文件系統(tǒng)model->setRootPath(QDir::currentPath());//設(shè)置根目錄ui->treeView->setModel(model);//設(shè)置數(shù)據(jù)模型ui->listView->setModel(model);//設(shè)置數(shù)據(jù)模型ui->tableView->setModel(model);//設(shè)置數(shù)據(jù)模型connect(ui->treeView, SIGNAL(clicked(QModelIndex)), ui->listView, SLOT(setRootIndex(QModelIndex)));connect(ui->treeView, SIGNAL(clicked(QModelIndex)), ui->tableView, SLOT(setRootIndex(QModelIndex)));
}ModelViewDialog::~ModelViewDialog()
{delete ui;
}void ModelViewDialog::on_treeView_clicked(const QModelIndex& index)
{ui->checkBox->setChecked(model->isDir(index));//是否目錄ui->labPath->setText(model->filePath(index));//返回節(jié)點(diǎn)的目錄名或帶路徑的文件名ui->labType->setText(model->type(index));//返回描述節(jié)點(diǎn)類型的文字ui->labFileName->setText(model->fileName(index));//返回去除路徑的文件夾名稱或文件名int sz = model->size(index)/1024;//如果節(jié)點(diǎn)是文件,返回文件大小的字節(jié)數(shù),如果節(jié)點(diǎn)是文件夾,返回0if(sz<1024) {ui->labSize->setText(QString("%1 KB").arg(sz));} else {ui->labSize->setText(QString::asprintf("%.1f MB", sz/1024.0));}
}
QStringListModel
QStringListModel用于處理字符串列表的數(shù)據(jù)模型,它可以作為QListView的數(shù)據(jù)模型,在界面上顯示和編輯字符串列表。
圖示
代碼示例
QStringListModelDialog.h
#ifndef QSTRINGLISTMODELDIALOG_H
#define QSTRINGLISTMODELDIALOG_H#include <QDialog>
#include <QStringListModel>namespace Ui
{class QStringListModelDialog;
}class QStringListModelDialog : public QDialog
{Q_OBJECTpublic:explicit QStringListModelDialog(QWidget* parent = nullptr);~QStringListModelDialog();private slots:void on_pushButtonAdd_clicked();void on_pushButtonInsert_clicked();void on_pushButtonRemove_clicked();void on_pushButtonClear_clicked();void on_pushButtonShow_clicked();void on_listView_clicked(const QModelIndex& index);void on_pushButtonReset_clicked();void on_pushButtonClearText_clicked();private:Ui::QStringListModelDialog* ui;QStringListModel* model;
};#endif // QSTRINGLISTMODELDIALOG_H
QStringListModelDialog.cpp
#include "QStringListModelDialog.h"
#include "ui_QStringListModelDialog.h"
QStringListModelDialog::QStringListModelDialog(QWidget* parent): QDialog(parent), ui(new Ui::QStringListModelDialog)
{ui->setupUi(this);QStringList theStrList;theStrList<<"北京"<<"上海"<<"天津"<<"河北"<<"河南"<<"山東";model = new QStringListModel(this);//將一個(gè)字符串列表的內(nèi)容作為數(shù)據(jù)模型的初始化數(shù)據(jù)內(nèi)容model->setStringList(theStrList);ui->listView->setModel(model);//雙擊 或 選擇并單擊后進(jìn)入編輯狀態(tài)ui->listView->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked);
}QStringListModelDialog::~QStringListModelDialog()
{delete ui;
}void QStringListModelDialog::on_pushButtonAdd_clicked()
{model->insertRow(model->rowCount());//在尾部插入一行QModelIndex index = model->index(model->rowCount()-1, 0);model->setData(index, "new item", Qt::DisplayRole);ui->listView->setCurrentIndex(index);//設(shè)置當(dāng)前選中的行
}void QStringListModelDialog::on_pushButtonInsert_clicked()
{QModelIndex index = ui->listView->currentIndex();model->insertRow(index.row());model->setData(index, "inserted item", Qt::DisplayRole);ui->listView->setCurrentIndex(index);
}void QStringListModelDialog::on_pushButtonRemove_clicked()
{QModelIndex index = ui->listView->currentIndex();model->removeRow(index.row());
}void QStringListModelDialog::on_pushButtonClear_clicked()
{model->removeRows(0, model->rowCount());
}void QStringListModelDialog::on_pushButtonShow_clicked()
{QStringList tmpList = model->stringList();ui->plainTextEdit->clear();for(int i=0; i<tmpList.count(); i++) {ui->plainTextEdit->appendPlainText(tmpList.at(i));}
}void QStringListModelDialog::on_listView_clicked(const QModelIndex& index)
{ui->label->setText(QString::asprintf("current:row=%d,column=%d", index.row(), index.column()));
}void QStringListModelDialog::on_pushButtonReset_clicked()
{model->removeRows(0, model->rowCount());QStringList theStrList;theStrList<<"北京"<<"上海"<<"天津"<<"河北"<<"河南"<<"山東";model->setStringList(theStrList);
}void QStringListModelDialog::on_pushButtonClearText_clicked()
{ui->plainTextEdit->clear();
}
QStandardItemModel
QStandardItemModel是標(biāo)準(zhǔn)的以項(xiàng)數(shù)據(jù)為基礎(chǔ)的標(biāo)準(zhǔn)數(shù)據(jù)模型類,通常與QTableView組合成Model/View結(jié)構(gòu)。實(shí)現(xiàn)通用的二維數(shù)據(jù)的管理功能。
圖示
代碼示例
QStandardItemModelDialog.h
#ifndef QSTANDARDITEMMODELDIALOG_H
#define QSTANDARDITEMMODELDIALOG_H#include <QDialog>
#include <QLabel>#include <QStandardItemModel>
#include <QItemSelectionModel>
#define FixedColumnCount 6
namespace Ui
{class QStandardItemModelDialog;
}class QStandardItemModelDialog : public QDialog
{Q_OBJECTpublic:explicit QStandardItemModelDialog(QWidget* parent = nullptr);~QStandardItemModelDialog();void initModelFromStringList(QStringList& fFileContent);public slots:void on_currentChanged(const QModelIndex& current, const QModelIndex& previous);private slots:void on_pushButtonOpen_clicked();void on_pushButtonAdd_clicked();void on_pushButtonInsert_clicked();void on_pushButtonRemove_clicked();void on_pushButtonLeft_clicked();void on_pushButtonMid_clicked();void on_pushButtonRight_clicked();void on_pushButtonBold_clicked();void on_pushButtonSaveAs_clicked();private:Ui::QStandardItemModelDialog* ui;QStandardItemModel* model;QLabel* labCurFile;QLabel* labCellPos;QLabel* labCellText;QItemSelectionModel* theSelection;};#endif // QSTANDARDITEMMODELDIALOG_H
QStandardItemModelDialog.cpp
#include "QStandardItemModelDialog.h"
#include "ui_QStandardItemModelDialog.h"
#include <QAbstractItemView>
#include <QFileDialog>
#include <QTextStream>QStandardItemModelDialog::QStandardItemModelDialog(QWidget* parent): QDialog(parent), ui(new Ui::QStandardItemModelDialog)
{ui->setupUi(this);model = new QStandardItemModel(0, FixedColumnCount, this);theSelection = new QItemSelectionModel(model);connect(theSelection, SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(on_currentChanged(QModelIndex, QModelIndex)));ui->tableView->setModel(model);ui->tableView->setSelectionModel(theSelection);ui->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);ui->tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
}QStandardItemModelDialog::~QStandardItemModelDialog()
{delete ui;
}void QStandardItemModelDialog::initModelFromStringList(QStringList& fFileContent)
{int rowCnt = fFileContent.count();model->setRowCount(rowCnt);QString header = fFileContent.at(0);//用一個(gè)或多個(gè)空白字符分隔 SkipEmptyParts函數(shù)會(huì)跳過任何空的部分QStringList headerList = header.split(QRegExp("\\s+"), QString::SkipEmptyParts);model->setHorizontalHeaderLabels(headerList);QStandardItem* item;QStringList tmpList;int j;for(int i=1; i<rowCnt; i++) {QString aLinText = fFileContent.at(i);tmpList = aLinText.split(QRegExp("\\s+"), QString::SkipEmptyParts);for(j=0; j<FixedColumnCount-1; j++) {item = new QStandardItem(tmpList.at(j));model->setItem(i-1, j, item);}item = new QStandardItem(headerList.at(j));item->setCheckable(true);if(tmpList.at(j) == "0") {item->setCheckState(Qt::Unchecked);} else {item->setCheckState(Qt::Checked);}model->setItem(i-1, j, item);}
}void QStandardItemModelDialog::on_currentChanged(const QModelIndex& current, const QModelIndex& previous)
{if(current.isValid()) {// labCellPos->setText(QString::asprintf("current: row=%d , col=%d", current.row(), current.column()));QStandardItem* item = model->itemFromIndex(current);// this->labCellText->setText("text:"+item->text());// QFont font = item->font();}
}void QStandardItemModelDialog::on_pushButtonOpen_clicked()
{QString curPath = QCoreApplication::applicationDirPath();QString aFileName = QFileDialog::getOpenFileName(this, u8"打開文件", curPath);if(aFileName.isEmpty()) {return;}QStringList fFileContent;QFile afile(aFileName);if(afile.open(QIODevice::ReadOnly |QIODevice::Text)) {QTextStream aStream(&afile);ui->plainTextEdit->clear();while(!aStream.atEnd()) {QString str = aStream.readLine();ui->plainTextEdit->appendPlainText(str);fFileContent.append(str);}afile.close();// labCurFile->setText("currentFile:"+aFileName);initModelFromStringList(fFileContent);}
}void QStandardItemModelDialog::on_pushButtonAdd_clicked()
{QList<QStandardItem*> itemList;QStandardItem* item;//不包含最后一列for(int i=0; i<FixedColumnCount-1; i++) {item = new QStandardItem("0");// item->setCheckable(true);itemList<<item;}QString str = model->headerData(model->columnCount()-1, Qt::Horizontal, Qt::DisplayRole).toString();item = new QStandardItem(str);item->setCheckable(true);itemList<<item;model->insertRow(model->rowCount(), itemList);QModelIndex curIndex = model->index(model->rowCount()-1, 0);theSelection->clearSelection();theSelection->setCurrentIndex(curIndex, QItemSelectionModel::Select);
}void QStandardItemModelDialog::on_pushButtonInsert_clicked()
{QList<QStandardItem*> itemList;QStandardItem* item;//不包含最后一列for(int i=0; i<FixedColumnCount-1; i++) {item = new QStandardItem("0");// item->setCheckable(true);itemList<<item;}QString str = model->headerData(model->columnCount()-1, Qt::Horizontal, Qt::DisplayRole).toString();item = new QStandardItem(str);item->setCheckable(true);itemList<<item;QModelIndex curIndex = theSelection->currentIndex();int curRow = curIndex.row();model->insertRow(curRow, itemList);curIndex = model->index(curRow, 0);theSelection->clearSelection();theSelection->setCurrentIndex(curIndex, QItemSelectionModel::Select);
}void QStandardItemModelDialog::on_pushButtonRemove_clicked()
{QModelIndex curIndex = theSelection->currentIndex();if(curIndex.row() == model->rowCount()-1) {model->removeRow(curIndex.row());} else {model->removeRow(curIndex.row());theSelection->setCurrentIndex(curIndex, QItemSelectionModel::Select);}
}void QStandardItemModelDialog::on_pushButtonLeft_clicked()
{if(!theSelection->hasSelection()) {return;}QModelIndexList selectedIndex = theSelection->selectedIndexes();for(int i=0; i<selectedIndex.count(); i++) {QModelIndex aIndex = selectedIndex.at(i);QStandardItem* item = model->itemFromIndex(aIndex);item->setTextAlignment(Qt::AlignLeft);}
}void QStandardItemModelDialog::on_pushButtonMid_clicked()
{if(!theSelection->hasSelection()) {return;}QModelIndexList selectedIndex = theSelection->selectedIndexes();for(int i=0; i<selectedIndex.count(); i++) {QModelIndex aIndex = selectedIndex.at(i);QStandardItem* item = model->itemFromIndex(aIndex);item->setTextAlignment(Qt::AlignCenter);}
}void QStandardItemModelDialog::on_pushButtonRight_clicked()
{if(!theSelection->hasSelection()) {return;}QModelIndexList selectedIndex = theSelection->selectedIndexes();for(int i=0; i<selectedIndex.count(); i++) {QModelIndex aIndex = selectedIndex.at(i);QStandardItem* item = model->itemFromIndex(aIndex);item->setTextAlignment(Qt::AlignRight);}
}void QStandardItemModelDialog::on_pushButtonBold_clicked()
{if(!theSelection->hasSelection()) {return;}QModelIndexList selectedIndex = theSelection->selectedIndexes();for(int i=0; i<selectedIndex.count(); i++) {QModelIndex aIndex = selectedIndex.at(i);QStandardItem* item = model->itemFromIndex(aIndex);QFont font = item->font();font.setBold(Qt::Checked);item->setFont(font);}
}void QStandardItemModelDialog::on_pushButtonSaveAs_clicked()
{QString curPath = QCoreApplication::applicationDirPath();QString aFileName = QFileDialog::getSaveFileName(this, u8"選擇一個(gè)文件", curPath);if(aFileName.isEmpty()) {return;}QFile file(aFileName);if(!(file.open(QIODevice::ReadWrite | QIODevice::Text| QIODevice::Truncate))) {return;}QTextStream aStream(&file);QStandardItem* item;int i, j;QString str;ui->plainTextEdit->clear();for(int i=0; i<model->columnCount(); i++) {item = model->horizontalHeaderItem(i);str = str +item->text()+"\t\t";}aStream<<str<<"\n";ui->plainTextEdit->appendPlainText(str);for(i=0; i<model->rowCount(); i++) {str="";for(j=0; j<model->columnCount()-1; j++) {item = model->item(i, j);str=str+item->text()+QString::asprintf("\t\t");}item = model->item(i, j);if(item->checkState() == Qt::Checked) {str = str+"1";} else {str = str+"0";}ui->plainTextEdit->appendPlainText(str);aStream<<str<<"\n";}
}
自定義代理
QAbstractItemDelegate是所有代理類的抽象基類,QStyledItemDelegate是視圖組件使用的缺省的代理類,QItemDelegate也是類似功能的類。
QStyledItemDelegate和QItemDelegate的差別在于前者可以使用樣式表設(shè)置來繪制組件,因此建議使用QStyledItemDelegate作為自定義代理類組件的基類。
無論從QStyledItemDelegate還是QItemDelegate繼承設(shè)計(jì)自定義代理組件,都會(huì)必須實(shí)現(xiàn)這四個(gè)函數(shù)。
- createEditor:創(chuàng)建用于編輯模型數(shù)據(jù)的widget組件,如QSpinBox組件;
- setEditorData:從數(shù)據(jù)模型獲取數(shù)據(jù),供widget組件進(jìn)行編輯;
- setModelData:將widget上的數(shù)據(jù)更新到數(shù)據(jù)模型;
- updateEditorGeometry:用于給widget組件設(shè)置一個(gè)合適的大小;
詳見我之前一段時(shí)間所寫的文章 文章地址