Qt 在菜单栏加入 CheckBox

方法一:设置action的checkable为true,触发toggled槽函数

代码

// .cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->actionShow->setCheckable(true);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_actionShow_toggled(bool arg1)
{
    QString text = arg1 ? "Show" : "Not show";
    ui->label->setText(text);
}
// .h
private slots:
    void on_actionShow_toggled(bool arg1);

效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

方法二:使用QWidgetAction

代码

// .h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QCheckBox>
#include <QWidgetAction>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_actionShow_toggled(bool arg1);
    void checkbox_clicked(bool arg1);

private:
    Ui::MainWindow *ui;
    QCheckBox *m_checkbox;
};
#endif // MAINWINDOW_H
// .cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow), m_checkbox(new QCheckBox(this))
{
    ui->setupUi(this);
    ui->actionShow->setCheckable(true);
	
	// QWidgetAction
    m_checkbox->setText("Check");
    QWidgetAction *action = new QWidgetAction(this);
    action->setDefaultWidget(m_checkbox);
    ui->menuFile->addActions(QList<QAction*>() << (qobject_cast<QAction*>(action))); // 没有直接加 QAction 的接口,只能绕一下
    connect(m_checkbox, SIGNAL(clicked(bool)), this, SLOT(checkbox_clicked(bool)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionShow_toggled(bool arg1)
{
    QString text = arg1 ? "Show" : "Not show";
    ui->label->setText(text);
}

void MainWindow::checkbox_clicked(bool arg1)
{
    QString text = arg1 ? "CheckBox Show" : "CheckBox not show";
    ui->label->setText(text);
}

效果

在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
QtQTableView中添加复选框可以通过自定义一个QItemDelegate来实现。下面是一个简单的示例: 首先,我们定义一个继承自QItemDelegate的自定义委托类,名称为CheckBoxDelegate: ```cpp class CheckBoxDelegate : public QItemDelegate { public: CheckBoxDelegate(QObject* parent = nullptr) : QItemDelegate(parent) { } void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override { if (index.column() == 0) { QStyleOptionButton checkBoxOption; checkBoxOption.rect = option.rect; checkBoxOption.state = index.data(Qt::CheckStateRole).toBool() ? QStyle::State_On : QStyle::State_Off; QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkBoxOption, painter); } else { QItemDelegate::paint(painter, option, index); } } QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override { if (index.column() == 0) { QCheckBox* checkBox = new QCheckBox(parent); return checkBox; } else { return QItemDelegate::createEditor(parent, option, index); } } void setEditorData(QWidget* editor, const QModelIndex& index) const override { if (index.column() == 0) { bool checked = index.data(Qt::CheckStateRole).toBool(); QCheckBox* checkBox = static_cast<QCheckBox*>(editor); checkBox->setChecked(checked); } else { QItemDelegate::setEditorData(editor, index); } } void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override { if (index.column() == 0) { QCheckBox* checkBox = static_cast<QCheckBox*>(editor); model->setData(index, checkBox->isChecked(), Qt::CheckStateRole); } else { QItemDelegate::setModelData(editor, model, index); } } }; ``` 然后,在我们的QTableView中使用这个自定义委托类来实现复选框的功能: ```cpp QTableView* tableView = new QTableView; QStandardItemModel* model = new QStandardItemModel; // 设置表格大小 model->setRowCount(3); model->setColumnCount(2); // 设置表头 model->setHeaderData(0, Qt::Horizontal, "复选框列"); model->setHeaderData(1, Qt::Horizontal, "其他列"); // 设置复选框数据 model->setData(model->index(0, 0), Qt::Checked, Qt::CheckStateRole); model->setData(model->index(1, 0), Qt::Unchecked, Qt::CheckStateRole); model->setData(model->index(2, 0), Qt::Checked, Qt::CheckStateRole); // 设置委托 CheckBoxDelegate* checkBoxDelegate = new CheckBoxDelegate(tableView); tableView->setItemDelegateForColumn(0, checkBoxDelegate); // 设置数据模型 tableView->setModel(model); ``` 上述代码创建了一个QTableView和一个QStandardItemModel,并通过数据模型设置了3行2列的数据。使用自定义委托类CheckBoxDelegate将第一列的数据设置为复选框。然后将数据模型设置到QTableView中。 最后,我们通过`tableView->setModel(model)`将数据模型设置到QTableView中完成复选框的添加。 这样就可以在QTableView中添加复选框了。记得在使用前先将相应的头文件导入:`#include <QCheckBox>`、`#include <QPainter>`、`#include <QStandardItemModel>`、`#include <QTableView>`、`#include <QItemDelegate>`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值