#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QRadioButton> #include <QButtonGroup> class MainWindow : public QMainWindow
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QRadioButton> #include <QButtonGroup> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); QRadioButton* radio0[10]; QRadioButton* radio1[10]; QButtonGroup* group[10]; }; #endif // MAINWINDOW_H
#include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { this->resize(400,300); this->centralWidget(); QString str0[] = {"筆記本","電話","印表機"}; QString str1[] = {"蘿蔔","白菜","青椒"}; group[0] = new QButtonGroup(this); group[1] = new QButtonGroup(this); int xpos = 10 , ypos = 30 ; for(int i=0;i<3;i++) { radio0[i] = new QRadioButton(str0[i],this); group[0]->addButton(radio0[i]); radio0[i]->setGeometry(xpos,ypos,100,30); radio1[i] = new QRadioButton(str1[i],this); group[1]->addButton(radio1[i]); radio1[i]->setGeometry(xpos+110,ypos,100,30); ypos += 50 ; } } MainWindow::~MainWindow() { }
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }