- 组件说明
QCheckBox:复选框类,同时提供选择多项的功能。
QRadioButton:单选框,一次只能选择一项的功能。
QGroupBox: 用来管理一组相互关联的选框按钮。
- 组件需要基本功能
Text: 组件显示文本
Icon: 组件的图标
Checkable: 组件是否可复选
Checked:按钮是否复选状态
autoExclusive: 在一个布局或一个容器组件内的同类按钮是否互斥。如果是互斥,当其中的一个按钮的checked 属性被设置为true 时,其他按钮的checked 属性被自动设置为false
void clicked(bool checked = false) // 点击按钮时
void pressed() //按下space 键或鼠标左键时
void released() //释放space 键或者鼠标左键时
void toggled(bool checked) //按钮的checked 属性变化时
- 组件使用需求
- 按钮选框和文本的位置,是文本内容显示在前还是按钮选项在前。
- 按钮选框和文本的样式处理。
- 代码显示结果
- 代码
-
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->demoBox1->setText("篮球"); //默认从左向右排 ui->demoBox1->setStyleSheet("QCheckBox{Spacing:30px;}"); ui->demoBox2->setText("足球"); ui->demoBox2->setLayoutDirection(Qt::RightToLeft);//从右向左排 ui->demoBox2->setStyleSheet("QCheckBox{Spacing:30px;}"); ui->radioButton_3->setText("男"); ui->radioButton_3->setStyleSheet("QRadioButton{Spacing:30px}"); ui->radioButton_4->setText("女"); ui->radioButton_4->setLayoutDirection(Qt::RightToLeft); ui->radioButton_4->setStyleSheet("QRadioButton{Spacing:60px;color:red}"); } //建立自动关联信号的槽 void MainWindow::on_radioButton_4_clicked(bool checked) { qDebug()<<"radioButton_4 click find "<<checked; } MainWindow::~MainWindow() { delete ui; }