【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);        // 第三种状态,半选中

法二:√

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

相关推荐
双叶8362 分钟前
(C语言)超市管理系统 (正式版)(指针)(数据结构)(清屏操作)(文件读写)
c语言·开发语言·数据结构·c++·windows
为美好的生活献上中指8 分钟前
java每日精进 5.14【参数校验】
java·开发语言·spring boot·tomcat
头疼的程序员29 分钟前
docker学习与使用(概念、镜像、容器、数据卷、dockerfile等)
学习·docker·容器
后青春期的诗go30 分钟前
基于Rust语言的Rocket框架和Sqlx库开发WebAPI项目记录(一)
开发语言·后端·rust
景天科技苑1 小时前
【Rust闭包】rust语言闭包函数原理用法汇总与应用实战
开发语言·后端·rust·闭包·闭包函数·rust闭包·rust闭包用法
LVerrrr2 小时前
Missashe考研日记—Day37-Day43
学习·考研
uyeonashi2 小时前
【Boost搜索引擎】构建Boost站内搜索引擎实践
开发语言·c++·搜索引擎
再睡一夏就好2 小时前
从硬件角度理解“Linux下一切皆文件“,详解用户级缓冲区
linux·服务器·c语言·开发语言·学习笔记
TIF星空3 小时前
【使用 C# 获取 USB 设备信息及进行通信】
开发语言·经验分享·笔记·学习·microsoft·c#
Smile丶凉轩5 小时前
Qt 界面优化(绘图)
开发语言·数据库·c++·qt