您好,登錄后才能下訂單哦!
本篇內容主要講解“QAbstractTableModel的使用方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“QAbstractTableModel的使用方法”吧!
QAbstractTableModel 結合 QTableView 使用,是QTableView的呈現的數據存儲。繼承QAbstractTableModel 必須重載以下幾個純虛函數(這些函數來自 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 函數用來填充QTableView 每一個單元格內容。其 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 行數據 */ struct TRowData { QString sItem1; QString sItem2; QString sItem3; QString sItem4; }; private: /** * @brief The THeaderData struct 表頭數據 */ struct THeaderData { Qt::Orientation eOrientation; ///< 水平方向或者豎直方向 QVariant oMetaData; ///< 原生數據 int iRole; ///< 表頭數據 }; public: explicit CTableModel(QObject *parent); void SetHeader(const QStringList &_clstHeader); void SetContentData(const QVector<TRowData> &_cvTableData); OVERLOAD_QT_CLASS_FUNCTION_DEGIN /** * @brief setHeaderData 該函數為QAbstractItemModel的虛函數, 其內部功能未實現, 若想使用需要手動重載 * @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; ///< 水平表頭數據 QHash<int, THeaderData> m_hashSection2VHeader; ///< 垂直表頭數據 QVector<TRowData> m_vtRowData; ///< 數據表 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(); } }
調用 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("數據%1_%2").arg(i).arg(1); tData.sItem2 = QString("數據%1_%2").arg(i).arg(2); tData.sItem3 = QString("數據%1_%2").arg(i).arg(3); tData.sItem4 = QString("數據%1_%2").arg(i).arg(4); vtModelData.push_back(tData); } m_pModel->SetContentData(vtModelData);
到此,相信大家對“QAbstractTableModel的使用方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。