1、说明
A、表头复选框实现三态复选框显示效果。
B、自定义信号void checkStateChanged(Qt::CheckState state); 表示复选框状态变化。
2、显示效果
实现单元格复选框居中请参照
QTableView 中设置复选框列居中_qtableview居中-CSDN博客
3、代码使用
QCheckBoxHeaderView* checkBoxHeaderView = new QCheckBoxHeaderView(0, Qt::Horizontal,ui->tableView);
ui->tableView->setHorizontalHeader(checkBoxHeaderView);
connect(checkBoxHeaderView, SIGNAL(checkStateChanged(Qt::CheckState)), this, SLOT(slotC
4、源代码
#ifndef QCHECKBOXHEADERVIEW_H
#define QCHECKBOXHEADERVIEW_H
#include <QObject>
#include <QHeaderView>
#include <QPainter>
class QCheckBoxHeaderView : public QHeaderView
{
Q_OBJECT
public:
QCheckBoxHeaderView(int nCheckColIndex, Qt::Orientation orientation, QWidget *parent = nullptr);
void SetCheckState(Qt::CheckState state);
protected:
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override;
void mousePressEvent(QMouseEvent *event) override;
signals:
void checkStateChanged(Qt::CheckState state);
private:
int m_nCheckColIndex; //复选框列下标
Qt::CheckState m_checkState;
};
#endif // QCHECKBOXHEADERVIEW_H
#include "QCheckBoxHeaderView.h"
#include <QMouseEvent>
#include <QApplication>
#include <QPainter>
#include <QStyle>
QCheckBoxHeaderView::QCheckBoxHeaderView(int nCheckColIndex, Qt::Orientation orientation, QWidget *parent):
QHeaderView (orientation, parent),m_nCheckColIndex(nCheckColIndex)
{
setSectionsClickable(true);
m_checkState = Qt::Unchecked;
}
void QCheckBoxHeaderView::SetCheckState(Qt::CheckState state)
{
m_checkState = state;
updateSection(m_nCheckColIndex);
}
void QCheckBoxHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();
if(m_nCheckColIndex == logicalIndex)
{
QStyleOptionButton option;
option.initFrom(this);
QPoint centerPt = rect.center();
option.rect = QRect(centerPt.x()-6, centerPt.y()-10, 19, 19);
option.features = QStyleOptionButton::None;
if(m_checkState == Qt::Checked)
{
option.state |= QStyle::State_On;;
}
else if(m_checkState == Qt::Unchecked)
{
option.state |= QStyle::State_Off;
}
else
{
option.state |= QStyle::State_NoChange;
}
style()->drawControl(QStyle::CE_CheckBox, &option, painter);
}
}
void QCheckBoxHeaderView::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
if(logicalIndexAt(event->pos()) == m_nCheckColIndex)
{
int xPos = sectionViewportPosition(m_nCheckColIndex);
int width = sectionSize(m_nCheckColIndex);
QRect sectionRect(xPos, 0, width, height());
QStyleOptionButton checkOption;
QRect checkBoxRect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkOption, nullptr);
checkBoxRect.moveCenter(sectionRect.center());
if(checkBoxRect.contains(event->pos()))
{
if(m_checkState == Qt::Checked)
{
m_checkState = Qt::Unchecked;
}
else if(m_checkState == Qt::Unchecked)
{
m_checkState = Qt::Checked;
}
else
{
m_checkState = Qt::Checked;
}
updateSection(m_nCheckColIndex);
emit checkStateChanged(m_checkState);
}
}
}
QHeaderView::mousePressEvent(event);
}