1.运行效果

2.代码
在Qt中,QTableView 通常与 QStandardItemModel 或其他模型一起使用来显示和管理数据。如果你想在 QTableView 的单元格中添加一个多选按钮(例如 QCheckBox),并且点击后能够改变其选中状态,你需要自定义一个委托(QStyledItemDelegate)来绘制和管理这个 QCheckBox。
下面是一个完整的示例,展示了如何在 QTableView 中添加和管理 QCheckBox:
#ifndef CHECKBOXDELEGATE_H
#define CHECKBOXDELEGATE_H
#include <QApplication>
#include <QTableView>
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include <QCheckBox>
class CheckBoxDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
CheckBoxDelegate(QObject *parent = nullptr)
: QStyledItemDelegate(parent)
{
}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
QStyleOptionButton checkBoxStyle;
checkBoxStyle.rect = option.rect;
checkBoxStyle.text = "";
checkBoxStyle.state = QStyle::State_Enabled | (index.data(Qt::CheckStateRole).toBool() ? QStyle::State_On : QStyle::State_Off);
QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkBoxStyle, painter);
}
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const override
{
QCheckBox *checkBox = new QCheckBox(parent);
checkBox->setCheckState(static_cast<Qt::CheckState>(index.data(Qt::CheckStateRole).toInt()));
connect(checkBox, &QCheckBox::stateChanged, this, [=](int state){
QModelIndex idx = index;
idx.data(Qt::CheckStateRole).setValue(static_cast<Qt::CheckState>(state));
});
return checkBox;
}
void setEditorData(QWidget *editor, const QModelIndex &index) const override
{
QCheckBox *checkBox = qobject_cast<QCheckBox*>(editor);
checkBox->setCheckState(static_cast<Qt::CheckState>(index.data(Qt::CheckStateRole).toInt()));
}
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
{
QCheckBox *checkBox = qobject_cast<QCheckBox*>(editor);
Qt::CheckState state = checkBox->checkState();
model->setData(index, state, Qt::CheckStateRole);
}
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const override
{
editor->setGeometry(option.rect);
}
};
#endif // CHECKBOXDELEGATE_H
#include "CheckBoxDelegate.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTableView tableView;
QStandardItemModel model(4, 2);
// Initialize model with some data
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 2; ++column) {
QModelIndex index = model.index(row, column, QModelIndex());
model.setData(index, QVariant((row+1)*(column+1)));
model.setData(index, Qt::Unchecked, Qt::CheckStateRole);
}
}
tableView.setModel(&model);
CheckBoxDelegate *delegate = new CheckBoxDelegate(&tableView);
tableView.setItemDelegateForColumn(1, delegate);
tableView.show();
return app.exec();
}
解释
- CheckBoxDelegate 类:
- 继承自
QStyledItemDelegate,用于自定义单元格的绘制和编辑。 paint方法:绘制QCheckBox。createEditor方法:创建一个QCheckBox编辑器小部件,并连接其状态变化信号。setEditorData和setModelData方法:在编辑器和模型之间同步数据。updateEditorGeometry方法:调整编辑器小部件的几何形状。
- 继承自
- main 函数:
- 创建一个
QTableView和QStandardItemModel。 - 初始化模型数据,并为每个单元格设置默认的未选中状态。
- 将
CheckBoxDelegate设置为第二列的委托。 - 显示
QTableView。
- 创建一个
运行这个程序,你会看到 QTableView 的第二列包含 QCheckBox,可以点击它们来切换选中状态。
1622

被折叠的 条评论
为什么被折叠?



