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

法二:√

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

相关推荐
heyCHEEMS2 分钟前
递归实现指数型枚举 Java
java·开发语言
这个懒人6 分钟前
C++实现文件断点续传:原理剖析与实战指南
开发语言·c++·文件备份·断点续传
景天科技苑6 分钟前
【Rust开发】Rust快速入门,开发出Rust的第一个Hello World
开发语言·后端·rust·rust入门·rust开发
进阶的小木桩15 分钟前
.NET 中的深拷贝实现方法
java·开发语言·.net
学地理的小胖砸18 分钟前
【python读取并显示遥感影像】
开发语言·python
DKPT27 分钟前
物联网与边缘计算之物联网架构(感知层、网络层、应用层)
大数据·笔记·学习·架构·系统架构
蒙奇D索大28 分钟前
【11408学习记录】英语语法精析:主从复合句之定语从句完全指南——从规则到实战例句一网打尽
笔记·学习·考研·改行学it
DKPT34 分钟前
物联网与边缘计算之边缘计算节点设计与协议(MQTT、CoAP)
服务器·笔记·学习·架构·系统架构
weifexie1 小时前
ruby超高级语法
开发语言·后端·ruby
麻芝汤圆1 小时前
利用Hadoop MapReduce实现流量统计分析
大数据·开发语言·hadoop·分布式·servlet·mapreduce