QTabView新增checkbox實現全選與反全選

QTabView新增checkbox實現全選與反全選

QHeaderView

標頭檔案

#ifndef ERPTABLEHEADERVIEW_H#define ERPTABLEHEADERVIEW_H#include class ErpTableHeaderView : public QHeaderView{ Q_OBJECTpublic: explicit ErpTableHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr); void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override; void mousePressEvent(QMouseEvent *e) override;signals: void tableViewCheckboxChange(Qt::CheckState checkState);private: Qt::CheckState mChkState = Qt::Unchecked;};#endif // ERPTABLEHEADERVIEW_H

實現檔案

#include “erptableheaderview。h”#include #include #include ErpTableHeaderView::ErpTableHeaderView(Qt::Orientation orientation, QWidget *parent) : QHeaderView(orientation, parent){}void ErpTableHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const{ painter->save(); QHeaderView::paintSection(painter, rect, logicalIndex); painter->restore(); if (logicalIndex == 0) { QStyleOptionButton chkButton; chkButton。state |= QStyle::State_Enabled; chkButton。rect = QRect(rect。left() + 5, rect。top() + 5, 25, 25); chkButton。state |= mChkState == Qt::Unchecked ? QStyle::State_Off : QStyle::State_On; QCheckBox chkbox; // 使用option指定的樣式選項,使用提供的油漆器繪製給定的原始元素 style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &chkButton, painter, &chkbox); // 使用提供的油漆器繪製給定元素,並使用選項指定的樣式選項 //chkbox。style()->drawControl(QStyle::CE_CheckBox, &chkButton, painter, &chkbox); }}void ErpTableHeaderView::mousePressEvent(QMouseEvent *e){ int column = logicalIndexAt(e->pos()); if (column == 0) { mChkState = mChkState == Qt::Unchecked ? Qt::Checked : Qt::Unchecked; emit tableViewCheckboxChange(mChkState); this->update(); } QHeaderView::mouseReleaseEvent(e);}

當滑鼠按下或釋放的時候傳送訊號

QTableView新增checkbox需要繼承QSqlTableModel,這塊在之前文章Qt自定義sql查詢語句封裝以及QTableModel使用sql有提到過,這裡加入了全選與反全選

標頭檔案

#ifndef ERPSQLTABLEMODEL_H#define ERPSQLTABLEMODEL_H#include class ErpSqlTableModel : public QSqlTableModel{ Q_OBJECTpublic: ErpSqlTableModel(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase()); bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; QVariant data(const QModelIndex &idx, int role = Qt::DisplayRole) const override; Qt::ItemFlags flags(const QModelIndex &index) const override; QString selectStatement() const override; void setSql(const QString& sqlstr); void disabledCheckbox();public slots: void tableViewCheckboxChange(Qt::CheckState checkState);private: QMap rowCheckMap; QString sql; bool needCheckBox = true;};#endif // ERPSQLTABLEMODEL_H

實現檔案

#include “erpsqltablemodel。h”#include #define CHECK_BOX_COLUMN 0ErpSqlTableModel::ErpSqlTableModel(QObject *parent, QSqlDatabase db) : QSqlTableModel(parent, db){}void ErpSqlTableModel::disabledCheckbox(){ needCheckBox = false;}bool ErpSqlTableModel::setData(const QModelIndex &index, const QVariant &value, int role){ if (!index。isValid()) return false; if (needCheckBox) { int actionColumn = index。column(); if (actionColumn == CHECK_BOX_COLUMN && role == Qt::CheckStateRole) { rowCheckMap[index。row()] = value。toInt() == Qt::Checked ? Qt::Checked : Qt::Unchecked; // The dataChanged() signal should be emitted if the data was successfully set。 emit dataChanged(index, index); } } return true;}QVariant ErpSqlTableModel::data(const QModelIndex &idx, int role) const{ if (!idx。isValid()) return QVariant(); if (needCheckBox) { int actionColumn = idx。column(); if (actionColumn == CHECK_BOX_COLUMN && role == Qt::CheckStateRole) { return rowCheckMap[idx。row()] == Qt::Checked ? Qt::Checked : Qt::Unchecked; } } return QSqlTableModel::data(idx, role);}Qt::ItemFlags ErpSqlTableModel::flags(const QModelIndex &index) const{ Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable; if (needCheckBox) { if (index。column() == CHECK_BOX_COLUMN) { flags |= Qt::ItemIsUserCheckable; } } return flags;}QString ErpSqlTableModel::selectStatement() const{ if (!sql。isEmpty()) { return sql; } return QSqlTableModel::selectStatement();}void ErpSqlTableModel::setSql(const QString& sqlstr){ sql = sqlstr;}void ErpSqlTableModel::tableViewCheckboxChange(Qt::CheckState checkState){ qDebug() << checkState; for (int i = 0; i < rowCount(); ++i) { QModelIndex idx = index(i, CHECK_BOX_COLUMN); setData(idx, checkState, Qt::CheckStateRole); }}

這裡注意的是在setData中,如果需要改變資料,則需要主動傳送訊號void QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles = 。。。),說明每當現有項中的資料發生變化時,就會發出此訊號。

使用

ErpTableHeaderView *headerView; ErpSqlTableModel *pageList;

headerView = new ErpTableHeaderView(Qt::Horizontal, this); pageList = entity->getPage(page, pageSize); ui->noticeList->setHorizontalHeader(headerView); connect(headerView, SIGNAL(tableViewCheckboxChange(Qt::CheckState)), pageList, SLOT(tableViewCheckboxChange(Qt::CheckState)));