您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“QAbstractTableModel的使用方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“QAbstractTableModel的使用方法”吧!
QAbstractTableModel 結(jié)合 QTableView 使用,是QTableView的呈現(xiàn)的數(shù)據(jù)存儲。繼承QAbstractTableModel 必須重載以下幾個純虛函數(shù)(這些函數(shù)來自 QAbstractTableModel 的父類 QAbstractItemModel) :
Q_INVOKABLE virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; Q_INVOKABLE virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; Q_INVOKABLE virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
其中 data 函數(shù)用來填充QTableView 每一個單元格內(nèi)容。其 role 雖為int類型,但實際的值為 Qt::ItemDataRole 枚舉類型 ,如下圖:
簡單的例子:
// tablemodel.h #include <QAbstractTableModel> #include <QStringList> #include <QVariant> #define OVERLOAD_QT_CLASS_FUNCTION_DEGIN #define OVERLOAD_QT_CLASS_FUNCTION_END class CTableModel : public QAbstractTableModel { Q_OBJECT public: /** * @brief The TRowData struct 行數(shù)據(jù) */ struct TRowData { QString sItem1; QString sItem2; QString sItem3; QString sItem4; }; private: /** * @brief The THeaderData struct 表頭數(shù)據(jù) */ struct THeaderData { Qt::Orientation eOrientation; ///< 水平方向或者豎直方向 QVariant oMetaData; ///< 原生數(shù)據(jù) int iRole; ///< 表頭數(shù)據(jù) }; public: explicit CTableModel(QObject *parent); void SetHeader(const QStringList &_clstHeader); void SetContentData(const QVector<TRowData> &_cvTableData); OVERLOAD_QT_CLASS_FUNCTION_DEGIN /** * @brief setHeaderData 該函數(shù)為QAbstractItemModel的虛函數(shù), 其內(nèi)部功能未實現(xiàn), 若想使用需要手動重載 * @param section * @param orientation * @param value * @param role * @return */ virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value,int role = Qt::EditRole) override; Q_INVOKABLE virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override; Q_INVOKABLE virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override; Q_INVOKABLE virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; Q_INVOKABLE virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; OVERLOAD_QT_CLASS_FUNCTION_END private: QHash<int, THeaderData> m_hashSection2HHeader; ///< 水平表頭數(shù)據(jù) QHash<int, THeaderData> m_hashSection2VHeader; ///< 垂直表頭數(shù)據(jù) QVector<TRowData> m_vtRowData; ///< 數(shù)據(jù)表 int m_iColumnCount; ///< 表頭字段 };
// tablemodel.cpp #include "tablemodel.h" CTableModel::CTableModel(QObject * parent) : QAbstractTableModel(parent) { } void CTableModel::SetHeader(const QStringList &_clstHeader) { m_iColumnCount = _clstHeader.size(); for (int i = 0; i < _clstHeader.size(); i++) { QString sHeader = _clstHeader.at(i); qDebug() << this->setHeaderData(i, Qt::Horizontal, sHeader, Qt::DisplayRole); } } void CTableModel::SetContentData(const QVector<CTableModel::TRowData> &_cvTableData) { QAbstractItemModel::beginResetModel(); m_vtRowData = _cvTableData; QAbstractItemModel::endResetModel(); } bool CTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) { if (section < m_iColumnCount) { THeaderData tData; tData.eOrientation = Qt::Horizontal; tData.iRole = Qt::DisplayRole; tData.oMetaData = value; m_hashSection2HHeader[section] = tData; emit headerDataChanged(Qt::Horizontal, section, section); return true; } else{ return false; } } int CTableModel::rowCount(const QModelIndex &parent) const { return m_vtRowData.size(); } int CTableModel::columnCount(const QModelIndex &parent) const { return m_iColumnCount; } QVariant CTableModel::data(const QModelIndex &index, int role) const { if (role == Qt::DisplayRole) { int iRow = index.row(); int iColumn = index.column(); if (iColumn < 4 && iRow < m_vtRowData.size()) { switch (iColumn) { case 0: return m_vtRowData[iRow].sItem1; case 1: return m_vtRowData[iRow].sItem2; case 2: return m_vtRowData[iRow].sItem3; case 3: return m_vtRowData[iRow].sItem4; default: return QVariant(); } } return QVariant(); } else if(role == Qt::TextAlignmentRole) { return Qt::AlignCenter; } return QVariant(); } QVariant CTableModel::headerData(int section, Qt::Orientation orientation, int role) const { if (section < m_iColumnCount) { switch (role){ case Qt::DisplayRole: { switch (orientation) { case Qt::Horizontal: { if (m_hashSection2HHeader.contains(section)) { return m_hashSection2HHeader[section].oMetaData; } else{ return QVariant(); } } case Qt::Vertical: default: return QVariant(); } } default: return QVariant(); } } else{ return QVariant(); } }
調(diào)用 CTableModel:
m_pModel = new CTableModel(ui->tableView); QStringList lstHeader = {"測試1", "測試1", "測試1", "測試1"}; m_pModel->SetHeader(lstHeader); ui->tableView->setModel(m_pModel); QVector<CTableModel::TRowData> vtModelData; for (int i = 0; i < 10; i++) { CTableModel::TRowData tData; tData.sItem1 = QString("數(shù)據(jù)%1_%2").arg(i).arg(1); tData.sItem2 = QString("數(shù)據(jù)%1_%2").arg(i).arg(2); tData.sItem3 = QString("數(shù)據(jù)%1_%2").arg(i).arg(3); tData.sItem4 = QString("數(shù)據(jù)%1_%2").arg(i).arg(4); vtModelData.push_back(tData); } m_pModel->SetContentData(vtModelData);
到此,相信大家對“QAbstractTableModel的使用方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。