在QTableView表头里面添加CheckBox,请用c++实现
时间: 2023-12-03 10:05:42 浏览: 102
好的,下面是在QTableView表头里面添加CheckBox的C++实现:
首先,我们需要自定义一个QHeaderView的子类CheckBoxHeader,并在其构造函数中设置一些属性:
```cpp
class CheckBoxHeader : public QHeaderView
{
Q_OBJECT
public:
CheckBoxHeader(Qt::Orientation orientation, QWidget *parent = nullptr)
: QHeaderView(orientation, parent)
{
setSectionResizeMode(QHeaderView::ResizeToContents);
setClickable(true);
setSortIndicatorShown(false);
}
private:
bool isChecked = false;
};
```
在paintSection()方法中,我们需要绘制CheckBox。因为我们只需要在第一列绘制CheckBox,所以只有logicalIndex为0时才进行绘制。在绘制时,我们可以使用QStyleOptionButton绘制CheckBox,并根据isChecked状态进行切换:
```cpp
void CheckBoxHeader::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
if (logicalIndex == 0) {
QStyleOptionButton option;
option.rect = QRect(10, 5, 20, 20);
option.state |= QStyle::State_Enabled | QStyle::State_Active;
if (isChecked) {
option.state |= QStyle::State_On;
} else {
option.state |= QStyle::State_Off;
}
style()->drawControl(QStyle::CE_CheckBox, &option, painter);
}
painter->restore();
}
```
在mousePressEvent()方法中,我们需要判断是否点击了CheckBox,并根据需要切换其状态。如果点击了CheckBox,我们需要发射一个信号sectionClicked(),以便在其他地方处理CheckBox的勾选状态:
```cpp
void CheckBoxHeader::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
if (logicalIndexAt(event->pos()) == 0) {
isChecked = !isChecked;
emit sectionClicked(0);
viewport()->update();
} else {
QHeaderView::mousePressEvent(event);
}
}
}
```
在使用QTableView时,我们只需要将其表头设置为自定义的CheckBoxHeader即可:
```cpp
QTableView *tableView = new QTableView(this);
CheckBoxHeader *header = new CheckBoxHeader(Qt::Horizontal, tableView);
tableView->setHorizontalHeader(header);
```
完整的代码如下:
```cpp
#include <QHeaderView>
#include <QMouseEvent>
#include <QPainter>
#include <QStyleOptionButton>
#include <QTableView>
class CheckBoxHeader : public QHeaderView
{
Q_OBJECT
public:
CheckBoxHeader(Qt::Orientation orientation, QWidget *parent = nullptr)
: QHeaderView(orientation, parent)
{
setSectionResizeMode(QHeaderView::ResizeToContents);
setClickable(true);
setSortIndicatorShown(false);
}
signals:
void sectionClicked(int logicalIndex);
protected:
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override
{
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
if (logicalIndex == 0) {
QStyleOptionButton option;
option.rect = QRect(10, 5, 20, 20);
option.state |= QStyle::State_Enabled | QStyle::State_Active;
if (isChecked) {
option.state |= QStyle::State_On;
} else {
option.state |= QStyle::State_Off;
}
style()->drawControl(QStyle::CE_CheckBox, &option, painter);
}
painter->restore();
}
void mousePressEvent(QMouseEvent *event) override
{
if (event->button() == Qt::LeftButton) {
if (logicalIndexAt(event->pos()) == 0) {
isChecked = !isChecked;
emit sectionClicked(0);
viewport()->update();
} else {
QHeaderView::mousePressEvent(event);
}
}
}
private:
bool isChecked = false;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTableView *tableView = new QTableView();
CheckBoxHeader *header = new CheckBoxHeader(Qt::Horizontal, tableView);
tableView->setHorizontalHeader(header);
tableView->setModel(new QStandardItemModel(4, 3, tableView));
tableView->show();
return a.exec();
}
#include "main.moc"
```
阅读全文