1 QTableWidget自定义表头QHeaderView加全选复选框
在使用QTableWidget时需要在表头添加全选复选框,但是默认的表头无法添加复选框,只能用图片画上去一个复选框了。并且实现点击复选框时,发出信号,让QTableWidget中每条记录的复选框都选中,取消时,QTableWidget中每条记录的复选框都取消。并且实现复选框的normal、hov、pressed三种状态的显示。三种状态需要设置背景图片。效果图如下
(1) 继承QHeaderView,重写绘制函数,绘制表头,重写鼠标移动函数,用来获取鼠标位置,鼠标移动到复选按钮上时,显示高亮。重写鼠标点击函数,点击复选框时,发送信号控制选中和取消。
头文件:
#pragma once
#include <QHeaderView>
#include <QPainter>
#include <QMouseEvent>
using namespace std;
class MyQHeaderView : public QHeaderView
{
Q_OBJECT
public:
explicit MyQHeaderView(Qt::Orientation orientation, QWidget *parent = 0);
signals:
void headCheckBoxToggled(bool checked);
protected:
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const;
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
private:
bool m_isOn;//是否选中
QPoint m_mousePoint;//鼠标位置
mutable QRect m_RectHeaderCheckBox;//复选框的位置
};
源文件
#include "QMyHeaderView.h"
MyQHeaderView::MyQHeaderView(Qt::Orientation orientation, QWidget *parent)
: QHeaderView(orientation, parent)
, m_isOn(false)
{
m_isOn = false;
m_mousePoint.setX(100);
m_mousePoint.setY(100);
}
/*绘制表头logicalIndex表示表头第几列,0表示第0列,也就是我们要画复选框的列,rect是表头第一列的大小*/
void MyQHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();
if (logicalIndex == 0)//第一列
{
//保存第一列的位置
m_RectHeaderCheckBox.setX(rect.x() + 3);
m_RectHeaderCheckBox.setY(rect.y() + 10);
m_RectHeaderCheckBox.setWidth(14);
m_RectHeaderCheckBox.setHeight(14);
QStyleOptionButton option;
QPixmap pix;
if (m_isOn)
{//图片需要在资源里添加
pix.load(":/checkImage/image/checkImage/check-sel.png");//选中
option.state = QStyle::State_On;
}
else if (m_RectHeaderCheckBox.contains(m_mousePoint))
{
pix.load(":/checkImage/image/checkImage/check-hov.png");//高亮
option.state = QStyle::State_HasFocus;
}
else
{
pix.load(":/checkImage/image/checkImage/check-nor.png");//正常
option.state = QStyle::State_Off;
}
style()->drawItemPixmap(painter, rect, Qt::AlignLeft | Qt::AlignVCenter, pix);
//方法2,画选择按钮,,无法设置背景图片
//
//option.rect = m_RectHeaderCheckBox;
//QIcon icon(pix);
//option.icon = icon;//setStyleSheet("background");
//style()->drawControl(QStyle::CE_CheckBox, &option, painter);
}
}
void MyQHeaderView::mousePressEvent(QMouseEvent *event)
{
//表头可控制,鼠标第一个列,鼠标在矩形范围内
if (isEnabled() && logicalIndexAt(event->pos()) == 0&&m_RectHeaderCheckBox.contains(event->pos()))
{
m_isOn = !m_isOn;
updateSection(0);//刷新表头第一列
emit headCheckBoxToggled(m_isOn);
}
else QHeaderView::mousePressEvent(event);
}
void MyQHeaderView::mouseMoveEvent(QMouseEvent *event)
{
//保存鼠标的位置
m_mousePoint = event->pos();
if (m_RectHeaderCheckBox.contains(m_mousePoint))
{
updateSection(0);//重绘表头复选框
}
QHeaderView::mouseMoveEvent(event);
}
(2) Qdesign中加入QTablewidget,并添加表头字段,第一个为空,用于添加选择复选框。
(3) 在主界面类中new一个表头对象,设置到QtableWidget中
//new表头对象
MyQHeaderView*myHeader = new MyQHeaderView(Qt::Horizontal, ui.tableWidget);
myHeader->setStretchLastSection(true);
myHeader->setStyleSheet("alignment: left;");
//连接单击信号槽,在槽函数中
QObject::connect(myHeader, &MyQHeaderView::headCheckBoxToggled, this, &AlarmCenter::SetAlarmListCheckState);
ui.tableWidget->setHorizontalHeader(myHeader);//设置表头
(4) 至于QTableWidget表格中数据前面的复选框则是用CheckBox
QCheckBox *CheckBox = new QCheckBox(ui.tableWidget);
QSize size(39, 35);
CheckBox->setFixedSize(size);
CheckBox->setCheckState(Qt::Unchecked);
//创建单击选中的信号槽连接
QObject::connect(CheckBox, &QCheckBox::clicked,this, [=]()
{
AlarmTableItemChoosed(row, 0);
});
ui.tableWidget->setCellWidget(row, 0, CheckBox);//row行,0列