【QT】常用控件——按钮组

继承Widget

PushButton

设置图片,先导入图片资源,见:【QT】资源文件导入_复制其他项目中的文件到qt项目中_StudyWinter的博客-CSDN博客

在布局中添加图片

调整尺寸

toolButton

显示图片、文本

显示图片(图片和文字都有时,显示图片)

显示文字

透明

RadioButton

单选按钮

四个中只能选一个,布局

默认选择一个,先改名

执行

捕获用户的选择

代码

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 单选按钮,默认选中男
    ui->rBtn_man->setChecked(true);
    // 监听用户选择女
    connect(ui->rBtn_woman, &QRadioButton::clicked, this, [=]() {
        qDebug() << "选择女";
    });
}

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

用户最终的选择

加一个性别参数

代码

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 单选按钮,默认选中男
    ui->rBtn_man->setChecked(true);
    this->gender = true;
    // 监听用户选择女

    connect(ui->rBtn_woman, &QRadioButton::clicked, this, [=]() {
        this->gender = false;
    });

    connect(ui->rBtn_man, &QRadioButton::clicked, this, [=]() {
        this->gender = true;
    });

    connect(ui->commit, &QRadioButton::clicked, this, [=]() {
        if (gender == true) {
            qDebug() << "选择的是男性";
        } else {
            qDebug() << "选择的是女性";
        }
    });


}

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

结果

CheckBox

复选按钮

监听价格实惠是否被选中,

法一:同样加属性

代码

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 单选按钮,默认选中男
    ui->rBtn_man->setChecked(true);
    this->gender = true;
    // 监听用户选择女

    connect(ui->rBtn_woman, &QRadioButton::clicked, this, [=]() {
        this->gender = false;
    });

    connect(ui->rBtn_man, &QRadioButton::clicked, this, [=]() {
        this->gender = true;
    });

    connect(ui->commit, &QRadioButton::clicked, this, [=]() {
        if (gender == true) {
            qDebug() << "选择的是男性";
        } else {
            qDebug() << "选择的是女性";
        }
    });




    connect(ui->checkBox_2, &QRadioButton::clicked, this, [=]() {
        str = "价格实惠";
    });

    connect(ui->commit, &QRadioButton::clicked, this, [=]() {
        if (str == "价格实惠") {
            qDebug() << "选择的是价格实惠";
        }
    });





}

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

结果

法二:

使用checkbox特有的信号

选中是2,取消是0

代码

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QCheckBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 单选按钮,默认选中男
    ui->rBtn_man->setChecked(true);
    this->gender = true;
    // 监听用户选择女

    connect(ui->rBtn_woman, &QRadioButton::clicked, this, [=]() {
        this->gender = false;
    });

    connect(ui->rBtn_man, &QRadioButton::clicked, this, [=]() {
        this->gender = true;
    });

    connect(ui->commit, &QRadioButton::clicked, this, [=]() {
        if (gender == true) {
            qDebug() << "选择的是男性";
        } else {
            qDebug() << "选择的是女性";
        }
    });




    connect(ui->checkBox_2, &QCheckBox::stateChanged, this, [=](int state) {
        qDebug() << state;
    });




}

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

法一:代码:怎么没有1呢,半选中状态

cpp 复制代码
ui->checkBox_2->setTristate(true);        // 第三种状态,半选中

法二:√

最好使用一种(代码或者控件),防止凌乱。

相关推荐
Jyywww121几秒前
微信小程序学习笔记
笔记·学习·微信小程序
amazinging3 分钟前
北京-4年功能测试2年空窗-报培训班学测开-第三十九天
python·学习·appium
每次的天空25 分钟前
Android-自定义View的实战学习总结
android·学习·kotlin·音视频
苦夏木禾27 分钟前
js请求避免缓存的三种方式
开发语言·javascript·缓存
重庆小透明32 分钟前
力扣刷题记录【1】146.LRU缓存
java·后端·学习·算法·leetcode·缓存
超级土豆粉35 分钟前
Turndown.js: 优雅地将 HTML 转换为 Markdown
开发语言·javascript·html
wei_shuo2 小时前
飞算 JavaAI 开发助手:深度学习驱动下的 Java 全链路智能开发新范式
java·开发语言·飞算javaai
熊猫钓鱼>_>2 小时前
用Python解锁图像处理之力:从基础到智能应用的深度探索
开发语言·图像处理·python
GO兔2 小时前
开篇:GORM入门——Go语言的ORM王者
开发语言·后端·golang·go
好开心啊没烦恼2 小时前
Python 数据分析:numpy,抽提,整数数组索引与基本索引扩展(元组传参)。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy·pandas