2-4-QCheckBox(单选框)、QRadioButton(复选框)

1、QCheckBox

为复选框

2、QRadioButton

为单选框
在这里插入图片描述
代码
widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QCheckBox>
#include <QRadioButton>
#include <QMenu>
#include <QDebug>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    //设置图标
     ui->toolButton->setIcon(QIcon("../timg.jpg"));//加载图标
     ui->toolButton->setIconSize(QSize(120,120));//设置图标大小 宽度像素x高度像素
    //复选框
     QMenu *menu=new QMenu(this);
     menu->addAction(QIcon("../timg2.jpg"),"树0",this,SLOT(doProcess()));
     menu->addAction(QIcon("../timg3.jpg"),"树1",this,"");
     menu->addAction(QIcon("../timg2.jpg"),"树2",this,"");
     menu->addAction(QIcon("../timg2.jpg"),"树3",this,"");
     menu->addAction(QIcon("../timg2.jpg"),"树4",this,"");
     menu->addAction(QIcon("../timg2.jpg"),"树5",this,"");
     menu->addAction(QIcon("../timg2.jpg"),"树6",this,"");
     ui->toolButton->setMenu(menu);

     //绑定信号与槽  checkbox复选框
     connect(ui->checkBox1,SIGNAL(clicked(bool)),this,SLOT(doProcessCheckBox()));
     connect(ui->checkBox2,SIGNAL(clicked(bool)),this,SLOT(doProcessCheckBox()));
     connect(ui->checkBox3,SIGNAL(clicked(bool)),this,SLOT(doProcessCheckBox()));

     //绑定信号与槽 radioButton
    connect(ui->radioButton1,SIGNAL(clicked(bool)),this,SLOT(doProcessRadioButton()));
    connect(ui->radioButton2,SIGNAL(clicked(bool)),this,SLOT(doProcessRadioButton()));
}

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

void Widget::doProcess(){
    qDebug()<<"222"<<endl;
}
void Widget::doProcessCheckBox(){
    QCheckBox *cbox=(QCheckBox*)this->sender();
    if(cbox->isChecked()){//当选中打印内容
        qDebug()<<cbox->text()<<endl;
    }
}
void Widget::doProcessRadioButton(){
    QRadioButton *rbtn=(QRadioButton*)this->sender();
    if(rbtn->isChecked()){
        qDebug()<<rbtn->text()<<endl;//当选中打印内容
    }
}



widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;
private slots:
    void doProcess();
    void doProcessCheckBox();
    void doProcessRadioButton();
};

#endif // WIDGET_H

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在QTableView中添加单选框框,您需要使用QItemDelegate类和setIndexWidget函数。以下是一个简单的示例: ```python from PyQt5.QtWidgets import QTableView, QItemDelegate, QRadioButton, QCheckBox class RadioButtonDelegate(QItemDelegate): def createEditor(self, parent, option, index): editor = QRadioButton(parent) editor.setChecked(index.data() == True) return editor def setEditorData(self, editor, index): editor.setChecked(index.data() == True) def setModelData(self, editor, model, index): model.setData(index, editor.isChecked(), role=QtCore.Qt.EditRole) class CheckBoxDelegate(QItemDelegate): def createEditor(self, parent, option, index): editor = QCheckBox(parent) editor.setChecked(index.data() == True) return editor def setEditorData(self, editor, index): editor.setChecked(index.data() == True) def setModelData(self, editor, model, index): model.setData(index, editor.isChecked(), role=QtCore.Qt.EditRole) ``` 我们创建了两个代理类:一个RadioButtonDelegate用于单选框,一个CheckBoxDelegate用于框。createEditor函数用于创建编辑器,setEditorData用于设置编辑器的当前值,setModelData用于将编辑器的值设置回模型中。 然后,您可以将这些代理分配给特定的列: ```python radio_delegate = RadioButtonDelegate() check_delegate = CheckBoxDelegate() table_view.setItemDelegateForColumn(1, radio_delegate) table_view.setItemDelegateForColumn(2, check_delegate) ``` 这将使第2列显示为单选框,第3列显示为框。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值