新聞中心
QT表格控件QTableView簡(jiǎn)介
表格視圖控件QTableView,需要和QStandardItemModel, 配套使用,這套框架是基于MVC設(shè)計(jì)模式設(shè)計(jì)的,M(Model)是QStandardItemModel數(shù)據(jù)模型,不能單獨(dú)顯示出來(lái)。V(view)是指QTableView視圖,要來(lái)顯示數(shù)據(jù)模型,C(controllor)控制在Qt中被弱化,與View合并到一起。
成都創(chuàng)新互聯(lián)總部坐落于成都市區(qū),致力網(wǎng)站建設(shè)服務(wù)有網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)絡(luò)營(yíng)銷(xiāo)策劃、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站維護(hù)、公眾號(hào)搭建、微信小程序開(kāi)發(fā)、軟件開(kāi)發(fā)等為企業(yè)提供一整套的信息化建設(shè)解決方案。創(chuàng)造真正意義上的網(wǎng)站建設(shè),為互聯(lián)網(wǎng)品牌在互動(dòng)行銷(xiāo)領(lǐng)域創(chuàng)造價(jià)值而不懈努力!
QTableView簡(jiǎn)單使用實(shí)例
QStandardItmeModel表格的數(shù)據(jù)模型,那么這個(gè)模型需要填上每一行每一列的數(shù)據(jù),就像execl表格一樣。
widget.h
#ifndef WIDGET_H #define WIDGET_H #include class Widget : public QTableView //繼承至QTableView { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); }; #endif // WIDGET_H
widget.cpp
#include "widget.h" #include#include Widget::Widget(QWidget *parent) : QTableView(parent) { QStandardItemModel* model = new QStandardItemModel(this); model->setItem(0, 0, new QStandardItem("張三")); model->setItem(0, 1, new QStandardItem("3")); model->setItem(0, 2, new QStandardItem("男")); this->setModel(model); } Widget::~Widget() { }
以上代碼實(shí)現(xiàn)了在model中添加一條數(shù)據(jù),然后通過(guò)setModel函數(shù)設(shè)置view的數(shù)據(jù)模型為model,顯示出來(lái),如圖:

QTableView修改行列字段名
修改字段名可以使用QStandardItemModel::setHeaderData,但是在這之前你需要調(diào)用QStandardItemModel::setColumnCount和QStandardItemModel::setRowCount,例如:
Widget::Widget(QWidget *parent)
: QTableView(parent)
{
QStandardItemModel* model = new QStandardItemModel(this);
/*設(shè)置列字段名*/
model->setColumnCount(3);
model->setHeaderData(0,Qt::Horizontal, "姓名");
model->setHeaderData(1,Qt::Horizontal, "年齡");
model->setHeaderData(2,Qt::Horizontal, "性別");
/*設(shè)置行字段名*/
model->setRowCount(3);
model->setHeaderData(0,Qt::Vertical, "記錄一");
model->setHeaderData(1,Qt::Vertical, "記錄二");
model->setHeaderData(2,Qt::Vertical, "記錄三");
/*設(shè)置一條數(shù)據(jù)*/
model->setItem(0, 0, new QStandardItem("張三"));
model->setItem(0, 1, new QStandardItem("3"));
model->setItem(0, 2, new QStandardItem("男"));
this->setModel(model);
}QTableView移除數(shù)據(jù)
移除數(shù)據(jù)的常用函數(shù)有:
/*移除某行數(shù)據(jù)*/ bool QAbstractItemModel::removeRow(int row, const QModelIndex &parent = QModelIndex()) /*移除某列數(shù)據(jù)*/ bool QAbstractItemModel::removeColumn(int column, const QModelIndex &parent = QModelIndex())
例如:
model->removeRow(0);//移除第0行數(shù)據(jù) model->removeColumn(0);//移除第0列數(shù)據(jù)
QTableView插入數(shù)據(jù)
插入一行數(shù)據(jù)
void QStandardItemModel::insertRow(int row, const QList&items) /* * row 表示從第幾行插入數(shù)據(jù) * items 表示要插入的數(shù)據(jù)QStandardItem對(duì)象 */
例如:
QListlist; list << new QStandardItem("王五") << new QStandardItem("22") << new QStandardItem("男"); model->insertRow(0, list); //在第0行插入一條記錄
QTableView數(shù)據(jù)變更信號(hào)處理
[signal] void QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector&roles = QVector ()); /* *topLeft bottomRight這兩索引指的是表格中被更改數(shù)據(jù)的區(qū)域,如果只有一個(gè)數(shù)據(jù)被更改,那么topLeft等于bottomRight */
例如:
widget.h
#ifndef WIDGET_H #define WIDGET_H #includeclass QStandardItemModel; class Widget : public QTableView { Q_OBJECT public slots: void dataChangedSlot(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles = QVector ()); public: Widget(QWidget *parent = 0); QStandardItemModel* _model; ~Widget(); }; #endif // WIDGET_H
widget.cpp
#include "widget.h" #include#include Widget::Widget(QWidget *parent) : QTableView(parent) { _model = new QStandardItemModel(this); _model->setColumnCount(3); _model->setHeaderData(0,Qt::Horizontal, "姓名"); _model->setHeaderData(1,Qt::Horizontal, "年齡"); _model->setHeaderData(2,Qt::Horizontal, "性別"); _model->setRowCount(3); _model->setHeaderData(0,Qt::Vertical, "記錄一"); _model->setHeaderData(1,Qt::Vertical, "記錄二"); _model->setHeaderData(2,Qt::Vertical, "記錄三"); _model->setItem(0, 0, new QStandardItem("張三")); _model->setItem(0, 1, new QStandardItem("3")); _model->setItem(0, 2, new QStandardItem("男")); connect(_model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector )), this, SLOT(dataChangedSlot(QModelIndex,QModelIndex,QVector ))); this->setModel(_model); } void Widget::dataChangedSlot(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles) { qDebug() << _model->data(topLeft).toString() << endl; } Widget::~Widget() { }
需要知道的是函數(shù)data可以獲取想要的QStandardItem對(duì)象的索引:
[pure virtual] QVariant QAbstractItemModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const
返回的QVariant對(duì)象是一種泛型變量,可以轉(zhuǎn)換成QString、int、double等數(shù)據(jù)類型。
QTableView常用函數(shù)
//默認(rèn)顯示行頭,如果你覺(jué)得不美觀的話,我們可以將隱藏 tableview->verticalHeader()->hide(); //設(shè)置選中時(shí)為整行選中 tableview->setSelectionBehavior(QAbstractItemView::SelectRows); //設(shè)置表格的單元為只讀屬性,即不能編輯 tableview->setEditTriggers(QAbstractItemView::NoEditTriggers); //返回一個(gè)被選中的所有Item的索引,一般是去遍歷這個(gè)鏈表進(jìn)行處理 [virtual protected] QModelIndexList QTableView::selectedIndexes() const
QStandardItem被點(diǎn)選信號(hào)
當(dāng)QStandardItemModel中的某個(gè)QStandardItem被點(diǎn)選后,QStandardItemModel對(duì)象會(huì)發(fā)出一個(gè)信號(hào):
void QAbstractItemView::clicked(const QModelIndex &index); /* *返回被點(diǎn)選的Item的索引 */
QItemDelegate代理
QTableView在處理信息顯示編輯的時(shí)候比較單調(diào),類似行編輯器,為了獲得更多的靈性性,交互通過(guò)QItemDelegate執(zhí)行。
下面通過(guò)派生一個(gè)SpinDelegate來(lái)實(shí)現(xiàn)一個(gè)整數(shù)旋轉(zhuǎn)框的代理器。
一般我們要重寫(xiě)函數(shù)createEditor:
[virtual] QWidget *QItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const /* *QWidget *parent一般是指哪個(gè)窗口使用了這個(gè)代理,一般用來(lái)托管內(nèi)存 * QStyleOptionViewItem &option 樣式風(fēng)格 * const QModelIndex &index 需要更改的Item索引 */
spinDelegate.h
#ifndef SPINDELEGATE_H #define SPINDELEGATE_H #includeclass SpinDelegate : public QItemDelegate { public: SpinDelegate(QObject *parent = Q_NULLPTR); QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; }; #endif // SPINDELEGATE_H
spinDelegate.cpp
#include "spindelegate.h" #includeSpinDelegate::SpinDelegate(QObject *parent): QItemDelegate(parent) { } QWidget* SpinDelegate::createEditor(QWidget *parent , const QStyleOptionViewItem &option, const QModelIndex &index) const { if(index.column() == 1) // 返回下拉框 { QSpinBox* box = new QSpinBox(parent); box->setMinimum(1); box->setMaximum(99); return box; } return QItemDelegate::createEditor(parent, option, index); }
將widget.cpp中的構(gòu)造器中修改如下函數(shù)如下:
Widget::Widget(QWidget *parent)
: QTableView(parent)
{
QStandardItemModel* model = new QStandardItemModel(this);
/*設(shè)置列字段名*/
model->setColumnCount(3);
model->setHeaderData(0,Qt::Horizontal, "姓名");
model->setHeaderData(1,Qt::Horizontal, "年齡");
model->setHeaderData(2,Qt::Horizontal, "性別");
/*設(shè)置行字段名*/
model->setRowCount(3);
model->setHeaderData(0,Qt::Vertical, "記錄一");
model->setHeaderData(1,Qt::Vertical, "記錄二");
model->setHeaderData(2,Qt::Vertical, "記錄三");
/*設(shè)置一條數(shù)據(jù)*/
model->setItem(0, 0, new QStandardItem("張三"));
model->setItem(0, 1, new QStandardItem("3"));
model->setItem(0, 2, new QStandardItem("男"));
this->setModel(model);
/*設(shè)置代理*/
this->setItemDelegate(new SpinDelegate(this));
}本文主要講解了QT表格控件QTableView詳細(xì)使用方法與實(shí)例,更多關(guān)于QT表格控件QTableView的使用技巧請(qǐng)查看下面的相關(guān)鏈接
網(wǎng)站標(biāo)題:QtGUI圖形圖像開(kāi)發(fā)之QT表格控件QTableView詳細(xì)使用方法與實(shí)例
標(biāo)題URL:http://www.dlmjj.cn/article/gepggc.html


咨詢
建站咨詢
