精美網(wǎng)站開發(fā)方案蘭州seo新站優(yōu)化招商
Qt 是一個跨平臺C++圖形界面開發(fā)庫,利用Qt可以快速開發(fā)跨平臺窗體應(yīng)用程序,在Qt中我們可以通過拖拽的方式將不同組件放到指定的位置,實現(xiàn)圖形化開發(fā)極大的方便了開發(fā)效率,本章將重點介紹QRadioButton
單選框組件以及與之交互的QButtonGroup
類的常用方法及靈活運用。
QRadioButton
是Qt框架中的一個部件(Widget),用于提供單選按鈕的界面元素。單選按鈕允許用戶從多個互斥的選項中選擇一個,通常用于表示一組相關(guān)但互斥的選項。
以下是QRadioButton
的一些常用方法,以表格形式概述:
方法 | 描述 |
---|---|
QRadioButton(QWidget *parent = nullptr) | 構(gòu)造函數(shù),創(chuàng)建一個單選按鈕,可指定父部件。 |
setText(const QString &text) | 設(shè)置單選按鈕的文本標(biāo)簽。 |
text() const | 獲取單選按鈕的文本標(biāo)簽。 |
setChecked(bool checked) | 設(shè)置單選按鈕的選中狀態(tài),true 表示選中,false 表示未選中。 |
isChecked() const | 判斷單選按鈕是否處于選中狀態(tài)。 |
setAutoExclusive(bool enabled) | 設(shè)置是否自動將同一組中的其他單選按鈕設(shè)為未選中狀態(tài)。 |
setObjectName(const QString &name) | 設(shè)置對象名稱,用于樣式表等。 |
setCheckedState(Qt::CheckState state) | 設(shè)置單選按鈕的選中狀態(tài),可選值有Qt::Checked 、Qt::Unchecked 和Qt::PartiallyChecked 。 |
checkState() const | 獲取單選按鈕的選中狀態(tài),返回Qt::Checked 、Qt::Unchecked 或Qt::PartiallyChecked 。 |
toggled(bool checked) | 信號,當(dāng)單選按鈕的選中狀態(tài)發(fā)生改變時觸發(fā)。參數(shù)checked 表示是否選中。 |
click() | 模擬點擊單選按鈕,觸發(fā)點擊事件。 |
setDisabled(bool disable) | 設(shè)置單選按鈕是否被禁用,true 表示禁用,false 表示啟用。 |
setEnabled(bool enable) | 設(shè)置單選按鈕是否啟用,true 表示啟用,false 表示禁用。 |
blockSignals(bool block) | 阻塞或解除阻塞信號與槽的連接,用于在某些操作時臨時禁用信號槽。 |
這些方法提供了對QRadioButton
的一些基本操作,包括設(shè)置文本、選中狀態(tài)、信號與槽等。通過這些方法,可以在應(yīng)用程序中方便地創(chuàng)建和控制單選按鈕??偠灾?#xff0c;QRadioButton
是一種簡單而有效的界面元素,用于在多個互斥的選項中進行單一選擇。
談到QRadioButton
組件就不得不提起QButtonGroup
類,因為這兩者通常是需要組合在一起使用的,一般來說QButtonGroup
用于管理一組按鈕,通常是單選按鈕(QRadioButton
)或復(fù)選按鈕(QCheckBox
)。它為這組按鈕提供了一些便捷的方法,方便進行管理和操作。
首先我們需要在mainwindow.h
頭文件中手動增加一個槽函數(shù)的聲明,該槽函數(shù)用于觸發(fā)后的處理工作。
private slots:void MySlots();
其次在主程序mainwindow.cpp
中我們通過new QBUttonGroup
新建一個按鈕組,并將其加入到group_sex
組內(nèi),創(chuàng)建信號和槽的綁定,將信號全部綁定到MySlots()
槽函數(shù)上,如下所示;
#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QMessageBox>
#include <QButtonGroup>
#include <iostream>// 定義全局組變量
QButtonGroup *group_sex;MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);// 將RadioButton放入ButtonGroup組中group_sex = new QButtonGroup(this);group_sex->addButton(ui->radioButton_male,0);group_sex->addButton(ui->radioButton_female,1);group_sex->addButton(ui->radioButton_unknown,2);// 設(shè)置默認選中ui->radioButton_unknown->setChecked(true);// 綁定信號和槽connect(ui->radioButton_male,SIGNAL(clicked(bool)),this,SLOT(MySlots()));connect(ui->radioButton_female,SIGNAL(clicked(bool)),this,SLOT(MySlots()));connect(ui->radioButton_unknown,SIGNAL(clicked(bool)),this,SLOT(MySlots()));
}MainWindow::~MainWindow()
{delete ui;
}// 手動創(chuàng)建一個槽函數(shù)
void MainWindow::MySlots()
{switch(group_sex->checkedId()){case 0:std::cout << "male" << std::endl;QMessageBox::information(nullptr, "信息", "用戶選中了男", QMessageBox::Ok);break;case 1:std::cout << "female" << std::endl;QMessageBox::information(nullptr, "信息", "用戶選中了女", QMessageBox::Ok);break;case 2:std::cout << "unknown" << std::endl;QMessageBox::information(nullptr, "信息", "用戶選中了未知", QMessageBox::Ok);break;}
}
當(dāng)程序運行后,讀者可自行選擇不同的單選框,此時會彈出不同的提示信息,如下圖;
當(dāng)然如果讀者不想使用QButtonGroup
對單選框進行分組操作,同樣可以實現(xiàn)判斷選中狀態(tài),通過依次檢查isChecked()
單選框的狀態(tài)即可實現(xiàn),但是此類方式并不推薦使用。
void MainWindow::on_pushButton_clicked()
{if(ui->radioButton_male->isChecked() == true){std::cout << "選中男" << std::endl;}if(ui->radioButton_female->isChecked() == true){std::cout << "選中女" << std::endl;}if(ui->radioButton_unknown->isChecked() == true){std::cout << "選中未知" << std::endl;}
}