Qt5之QRadioButton,qt5qradiobutton

來源:互聯網
上載者:User

Qt5之QRadioButton,qt5qradiobutton

本常式介紹QRadioButton的使用,包括QRadioButton的分組、多個QRadioButton控制項響應同一個槽函數、QRadioButton的ID設定從而避免繁瑣的判斷。

一、在UI介面添加如下控制項:


二、對QRadioButton控制項進行分組

      QRadioButton的分組有多重方法,如採用組合框、QWidge等,下面介紹採用QButtonGroup方法來實現分組,好處是不影響QRadioButton在介面上的顯示(組合框分組方式會在介面上出現組合框,要以自己的需要選擇),以及方便ID的設定。

     首先添加標頭檔:

    #include <QButtonGroup>
     聲明QButtonGroup變數

    QButtonGroup *btnGroupFruits;    QButtonGroup *btnGroupVegetables;
    在表單建構函式中初始化QButtonGroup,以及把相應的QRadioButton添加進來並設定ID

    btnGroupFruits = new QButtonGroup(this);    btnGroupFruits->addButton(ui->radioButton11, 0);    btnGroupFruits->addButton(ui->radioButton12, 1);    btnGroupFruits->addButton(ui->radioButton13, 2);    ui->radioButton11->setChecked(true);    btnGroupVegetables = new QButtonGroup(this);    btnGroupVegetables->addButton(ui->radioButton21, 0);    btnGroupVegetables->addButton(ui->radioButton22, 1);    btnGroupVegetables->addButton(ui->radioButton23, 2);    ui->radioButton21->setChecked(true);
三、多個QRadioButton控制項響應同一個槽函數

     在標頭檔聲明槽函數:

public slots:    void onRadioClickFruits();    void onRadioClickVegetables();
    在表單建構函式中綁定訊號與槽:

    connect(ui->radioButton11, SIGNAL(clicked()), this, SLOT(onRadioClickFruits()));    connect(ui->radioButton12, SIGNAL(clicked()), this, SLOT(onRadioClickFruits()));    connect(ui->radioButton13, SIGNAL(clicked()), this, SLOT(onRadioClickFruits()));    connect(ui->radioButton21, SIGNAL(clicked()), this, SLOT(onRadioClickVegetables()));    connect(ui->radioButton22, SIGNAL(clicked()), this, SLOT(onRadioClickVegetables()));    connect(ui->radioButton23, SIGNAL(clicked()), this, SLOT(onRadioClickVegetables()));
   槽函數的實現:

   QRadioButton的槽函數中,不需要逐個檢查QRadioButton控制項狀態,僅僅通過btnGroupFruits->checkedId()來獲知哪一個QRadioButton控制項被選中,其返回被選中控制項的ID值。

void MainWindow::onRadioClickFruits(){   switch(btnGroupFruits->checkedId())   {   case 0:       qDebug() << QString::fromLocal8Bit("蘋果");       break;   case 1:       qDebug() << QString::fromLocal8Bit("西紅柿");       break;   case 2:       qDebug() << QString::fromLocal8Bit("芒果");       break;   }}void MainWindow::onRadioClickVegetables(){    switch(btnGroupVegetables->checkedId())    {    case 0:        qDebug() << QString::fromLocal8Bit("馬鈴薯");        break;    case 1:        qDebug() << QString::fromLocal8Bit("青椒");        break;    case 2:        qDebug() << QString::fromLocal8Bit("菠菜");        break;    }}
以下是程式運行結果:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.