QTableWidget表头添加复选框实现全选功能
QTableWidget表头添加复选框实现全选功能
主要是CheckBoxHeaderView继承QHeaderView,重写paintSection函数
void checkStatusChange(bool);
是复选框是否选中的信号。
详细代码如下:
#ifndef CHECKBOXHEADERVIEW_H #define CHECKBOXHEADERVIEW_H #include <QtGui> #include <QPainter> #include <QHeaderView> #include <QStyleOptionButton> #include <QStyle> #include <QCheckBox> /// 复选框表头 class CheckBoxHeaderView : public QHeaderView { Q_OBJECT private: int m_checkColIndex; //列下标 QPoint m_topLeft; //勾选框起始坐标 QSize m_checkSize; //勾选框大小 bool m_isChecked; //勾选框状态 public: CheckBoxHeaderView( int checkColumnIndex, QPoint topLeft, QSize size, Qt::Orientation orientation, QWidget * parent = 0) : QHeaderView(orientation, parent) { m_checkColIndex = checkColumnIndex; m_topLeft = topLeft; m_checkSize = size; m_isChecked = false; } void setCheckState(bool state) { m_isChecked = state; } protected: void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const { painter->save(); QHeaderView::paintSection(painter, rect, logicalIndex); painter->restore(); if (logicalIndex == m_checkColIndex) { QStyleOptionButton option; int width = 10; for (int i=0; i<logicalIndex; ++i) { width += sectionSize( i ); } option.rect = QRect(m_topLeft.x(), m_topLeft.y(), m_checkSize.width(), m_checkSize.height()); if (m_isChecked) { option.state = QStyle::State_On; } else { option.state = QStyle::State_Off; } QCheckBox *check = new QCheckBox; QString sheet = QString("QCheckBox::indicator {width: %1px; height: %2px;}").arg(m_checkSize.width()).arg(m_checkSize.height()); check->setStyleSheet(sheet); this->style()->drawControl(QStyle::CE_CheckBox, &option, painter, check); } } void mousePressEvent(QMouseEvent *event) { if (visualIndexAt(event->pos().x()) == m_checkColIndex) { m_isChecked = !m_isChecked; this->updateSection(m_checkColIndex); emit checkStatusChange(m_isChecked); } QHeaderView::mousePressEvent(event); } signals: void checkStatusChange(bool); }; #endif // CHECKBOXHEADERVIEW_H
写个例子:
m_checkHeaderView = new CheckBoxHeaderView(0, QPoint(26 , 13),QSize(20 , 20 ), Qt::Horizontal, this); m_checkHeaderView->setObjectName(QStringLiteral("m_checkHeaderView")); ui->tbwReportNumberAdptive->setHorizontalHeader( m_checkHeaderView ); //绑定信号和槽,然后就可以实现全选功能了 connect(m_checkHeaderView, &CheckBoxHeaderView::checkStatusChange, this, &frmReport::slotCheckStatusChange);