반응형
QRadioButton
: QCheckBox 위젯과 사용법이 거의 같지만, 단일 선택만 가능하다.
예를 들어, 네 가지 항목 중 하나만 선택할 경우 이 위젯을 사용하면 유용하다.
QRadioButton을 QGroupBox와 함께 사용
-> 하나의 그룹으로 설정하면, 해당 그룹 안에서만 단일 선택이 되도록 설정이 가능하다.
<QRadioButton위젯을 이용해 단일 선택하기>
예제 결과 2 캡쳐 이미지를 참고하자
두 번재 그룹 Radio Button Group 2는 첫 번째 그룹과 달리 선택할 수 있는 항목이 하나 더 있다.
그리고 이 그룹에 전체를 선택하지 못하게 하는 기능이 있다.
if Radio Button Group2 체크 -> 속한 위젯 활성화
Radio Button Group2 언체크 -> 속한 모든 위젯을 선택하지 못하도록 비활성화
// mywidget.h
#include <QDialog> class QGroupBox; class QRadioButton; class QHBoxLayout; class MyWidget : public QDialog { Q_OBJECT public : MyWidget(); private : QGroupBox *gb1; QRadioButton *radio1_1; QRadioButton *radio1_2; QRadioButton *radio1_3; QRadioButton *radio1_4; QGroupBox *gb2; QRadioButton *radio2_1; QRadioButton *radio2_2; QRadioButton *radio2_3; QRadioButton *radio2_4; QHBoxLayout *layout; private slots : void eventChange1(); void eventChange2(); };
// mywidget.cpp
#include <QtGui> #include "mywidget.h" MyWidget::MyWidget() { setFixedSize(500,200); gb1 = new QGroupBox("Radio Button Group 1"); radio1_1 = new QRadioButton(tr("&Radio Button 1")); radio1_2 = new QRadioButton(tr("&Radio Button 2")); radio1_3 = new QRadioButton(tr("&Radio Button 3")); radio1_4 = new QRadioButton(tr("&Radio Button 4")); radio1_1->setChecked(true); QVBoxLayout *vbox1 = new QVBoxLayout; vbox1->addWidget(radio1_1); vbox1->addWidget(radio1_2); vbox1->addWidget(radio1_3); vbox1->addWidget(radio1_4); vbox1->addStretch(1); // addStretch : 늘어나는 공백을 추가해준다. // 최소 크기는 0이고, 늘림 인자는(stretch factor)stretch의 기본값은 0이다. // 늘림 인자란 : 위젯 사이의 늘어나는 비율 // ex ) 버튼 두 개가 있을 대, 하나의 늘림인자는 1이고 다른 하나는 3이면 // 창이 늘어날 때 1:3의 비율로 버튼이 늘어난다. gb1->setLayout(vbox1); // -- 두 번째 그룹 Radio Button Group2 gb2 = new QGroupBox("Radio Button Group 2"); gb2->setCheckable(true); // 이 그룹에 속한 모든 위젯을 활성화시키거나 비활성화시킬 수 있는 Check옵션을 추가할 수 있다. gb2->setChecked(true); // QRadioButton 위젯 중 해당 위젯을 체크 상태로 표시할 수 있는 기능 radio2_1 = new QRadioButton(tr("&Radio button 1")); radio2_2 = new QRadioButton(tr("&Radio button 2")); radio2_3 = new QRadioButton(tr("&Radio button 3")); radio2_4 = new QRadioButton(tr("&Radio button 4")); radio2_1->setChecked(true); static QIcon Img(QPixmap("./images/icon.png")); radio2_1->setIcon(Img); radio2_2->setIcon(Img); radio2_3->setIcon(Img); radio2_4->setIcon(Img); QVBoxLayout *vbox2 = new QVBoxLayout; vbox2->addWidget(radio2_1); vbox2->addWidget(radio2_2); vbox2->addWidget(radio2_3); vbox2->addWidget(radio2_4); vbox2->addStretch(1); gb2->setLayout(vbox2); layout = new QHBoxLayout; layout->addWidget(gb1); layout->addWidget(gb2); connect(radio1_1, SIGNAL(clicked()), this, SLOT(eventChange1())); connect(radio1_2, SIGNAL(clicked()), this, SLOT(eventChange1())); connect(radio1_3, SIGNAL(clicked()), this, SLOT(eventChange1())); connect(radio1_4, SIGNAL(clicked()), this, SLOT(eventChange1())); connect(radio2_1, SIGNAL(clicked()), this, SLOT(eventChange2())); connect(radio2_2, SIGNAL(clicked()), this, SLOT(eventChange2())); connect(radio2_3, SIGNAL(clicked()), this, SLOT(eventChange2())); connect(radio2_4, SIGNAL(clicked()), this, SLOT(eventChange2())); setLayout(layout); } void MyWidget::eventChange1() { qDebug("----- Radio Button Group 1 Checkecd State -----"); if(radio1_1->isChecked()) { qDebug("Radio1_1 Checked."); } else { qDebug("Radio1_1 UnChecked."); } if(radio1_2->isChecked()) { qDebug("Radio1_2 Checked."); } else { qDebug("Radio1_2 UnChecked."); } if(radio1_3->isChecked()) { qDebug("Radio1_3 Checked."); } else { qDebug("Radio1_3 UnChecked."); } if(radio1_4->isChecked()) { qDebug("Radio1_4 Checked."); } else { qDebug("Radio1_4 UnChecked."); } } void MyWidget::eventChange2() { qDebug("----- Radio Button Group 2 Checked State -----"); if(gb2->isChecked()) { qDebug("Radio Button Group 2 Checked."); } else { qDebug("Radio Button Group 2 UnChecked."); } if(radio2_1->isChecked()) { qDebug("Radio2_1 Checked."); } else { qDebug("Radio2_1 UnChecked."); } if(radio2_2->isChecked()) { qDebug("Radio2_2 Checked."); } else { qDebug("Radio2_2 UnChecked."); } if(radio2_3->isChecked()) { qDebug("Radio2_3 Checked."); } else { qDebug("Radio2_3 UnChecked."); } if(radio2_4->isChecked()) { qDebug("Radio2_4 Checked."); } else { qDebug("Radio2_4 UnChecked."); } }
// main.cpp
#include <QApplication> #include <QStyleFactory> #include "mywidget.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWidget widget; QStringList styles = QStyleFactory::keys(); app.setStyle(styles[3]); widget.show(); return app.exec(); }
반응형