qt QTableView中添加一个多选按钮,这个多选安装可以点击,但点击后要有变化,一次点击选中,再一次点击取消

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();
}

解释

  1. CheckBoxDelegate 类
    • 继承自 QStyledItemDelegate,用于自定义单元格的绘制和编辑。
    • paint 方法:绘制 QCheckBox
    • createEditor 方法:创建一个 QCheckBox 编辑器小部件,并连接其状态变化信号。
    • setEditorData 和 setModelData 方法:在编辑器和模型之间同步数据。
    • updateEditorGeometry 方法:调整编辑器小部件的几何形状。
  2. main 函数
    • 创建一个 QTableView 和 QStandardItemModel
    • 初始化模型数据,并为每个单元格设置默认的未选中状态。
    • 将 CheckBoxDelegate 设置为第二列的委托。
    • 显示 QTableView

运行这个程序,你会看到 QTableView 的第二列包含 QCheckBox,可以点击它们来切换选中状态。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值