QRadioButton 如果对您有用记得点赞哦)
QRadioButton 是一个可以切换选中(checked)或未选中(unchecked)状态的选项按钮。
单选框通常呈现给用户一个“多选一”的选择。也就是说,在一组单选框中,一次只能选中一
个单选框。默认在同一个父对象下,初始化后点击它们是互斥状态。
例子:
头文件“mainwindow.h”具体代码如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
/* 引入QRadioButton */
#include <QRadioButton>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
/* 声明两个QRadioButton对象 */
QRadioButton *radioButton1;
QRadioButton *radioButton2;
};
#endif // MAINWINDOW_H
mainwindow.cpp”具体代码如下
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
/* 主窗体设置位置和显示的大小 */
this->setGeometry(0, 0, 800, 480);
/* 设置背景颜色黑色 */
this->setStyleSheet("QMainWindow {background-color: rgba(0, 0, 0, 100%);}");
/* 实例化对象 */
radioButton1 = new QRadioButton(this);
radioButton2 = new QRadioButton(this);
/* 设置两个QRadioButton的位置和显示大小 */
radioButton1->setGeometry(300, 200, 100, 50);
radioButton2->setGeometry(400, 200, 100, 50);
/* 设置两个QRadioButton的显示文本 */
radioButton1->setText("开关一");
radioButton2->setText("开关二");
/* 设置初始状态,radioButton1的Checked为false,另一个为true*/
radioButton1->setChecked(false);
radioButton2->setChecked(true);
}
MainWindow::~MainWindow()
{
}
按钮图片:
运行效果: