QRadioButton

QRadioButton和QCheckBox都是選單元件,不過使用者能在QCheckBox進行複選,在QRadioButton只能選擇一個。這邊依序將3個QRadioButton加在布局管理員中,再將布局管理員加在QGroupBox內,形成一組按鍵扭的形式。


widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QRadioButton>
#include <QGroupBox>
#include <QVBoxLayout>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
private:
    QVBoxLayout *myLayout;
    QGroupBox *myGroupBox;
    QRadioButton *check1;
    QRadioButton *check2;
    QRadioButton *check3;
};

#endif 

widget.cpp

#include "widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent)
{  
    setWindowTitle(tr("按鍵組"));
    myGroupBox = new QGroupBox(tr("這是一組按鍵"),this);
    myGroupBox->setGeometry(30,30,100,100);
    check1 = new QRadioButton(tr("按鍵1"),this);
    check1->setChecked(true);                //設定check1為打勾狀態
    check2 = new QRadioButton(tr("按鍵2"),this);
    check3 = new QRadioButton(tr("按鍵3"),this);
    myLayout = new QVBoxLayout;
    myLayout->addWidget(check1);
    myLayout->addWidget(check2);
    myLayout->addWidget(check3);
    myGroupBox->setLayout(myLayout);
}

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

QRadioButton