当前位置:网站首页>Qt中radioButton使用
Qt中radioButton使用
2022-06-11 11:33:00 【声希Censh】
Qt中radioButton使用
先在ui界面上拖几个radioButton控件,如图(加了个Group Box):
直接上代码:
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QAbstractButton>
//QAbstractButton类是按钮部件的抽象基类,提供了按钮所共有的功能.
//QAbstractButton提供了点击和勾选按钮。QRadioButton和QCheckBox类只提供了勾选按钮
#include <QButtonGroup> //
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
QButtonGroup *btnGroup; //添加buttongroup
public slots:
void onButtonClicked(QAbstractButton *button);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//radioButton
btnGroup= new QButtonGroup(this);
btnGroup->addButton(ui->radioBtnV1);
btnGroup->addButton(ui->radioBtnV2);
btnGroup->addButton(ui->radioBtnV3);
btnGroup->addButton(ui->radioBtnV4);
btnGroup->setId(ui->radioBtnV1,0);
btnGroup->setId(ui->radioBtnV2,1);
btnGroup->setId(ui->radioBtnV3,2);
btnGroup->setId(ui->radioBtnV4,3);
ui->radioBtnV1->setChecked(1); //默认选中第一个
//ButtonGroup信号链接
connect(btnGroup, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(onButtonClicked(QAbstractButton*))); //信号输出选择
}
void MainWindow::onButtonClicked(QAbstractButton *button) //radio button选择
{
Q_UNUSED(button)
int sigChoose = btnGroup->checkedId(); //选择信号
if(sigChoose==0) qDebug() << sigChoose;
else if(sigChoose==1) qDebug() << sigChoose;
else if(sigChoose==2) qDebug() << sigChoose;
else if(sigChoose==3) qDebug() << sigChoose;
}
MainWindow::~MainWindow()
{
delete ui;
}
边栏推荐
- typescript 编译选项和配置文件
- WordPress regenerate featured image plugin: regenerate thumbnails
- Gerber文件在PCB制造中的作用
- [issue 30] shopee golang development experience
- CVPR 2022 | 文本引导的实体级别图像操作ManiTrans
- Dominating set, independent set, covering set
- Etcd introduction
- Create a folder in the WordPress Library
- Test cos HTML cache static cache plug-in
- Runtime reconfiguration of etcd
猜你喜欢

Zhejiang University and Microsoft Asia Research Institute released a new method of video recognition, which can recognize video frame by frame without data marking, or can be used for sign language tr

Only when you find your own advantages can you work tirelessly and get twice the result with half the effort!

01_ Description object_ Class diagram

Liufan, CFO of papaya mobile, unleashes women's innovative power in the digital age

The role of Gerber file in PCB manufacturing

MyCat-分库分表

web开发选型,web开发毕业谁

Adapter mode -- can you talk well?
![[file upload vulnerability 06] server file content detection and bypass experiment + image horse production method (based on upload-labs-14 shooting range)](/img/30/79516390c2b2b50a224eaa84a0c1c9.jpg)
[file upload vulnerability 06] server file content detection and bypass experiment + image horse production method (based on upload-labs-14 shooting range)

ELK - ElastAlert最大的坑
随机推荐
WP Super Cache静态缓存插件简明使用教程
Template engine - thymeleaf
01_ Description object_ Class diagram
Network protocol of yyds dry goods inventory: datagram socket for detailed explanation of socket protocol
ELK - ElastAlert最大的坑
my. Binlog startup failure caused by the difference between [mysql] and [mysqld] in CNF
17.4创建多个线程、数据共享问题分析与案例代码
Mongodb usage
中文输入法输入事件composition的使用
再不刷题就晚了,最全的BAT大厂面试题整理
17.5 concept, usage, deadlock demonstration and solution of mutex
Intl.numberformat set number format
【Go】Gin源码解读
Web development model selection, who graduated from web development
No category parents插件帮你去掉分类链接中的category前缀
Uncaught typeerror: cannot set property 'next' of undefined
统计出现次数最多的前K个字符串
普通人应当如何挑选年金险产品?
Bark – 自己给自己的 iPhone 发推送提醒 – 最简单的推送提醒服务,开源免费
推荐几款Gravatar头像缓存插件