Qt5-控件-QRadioButton-单选按钮-用于从多个选项中选取一个-单选神器

#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();
}

posted @ 2016-02-22 22:22  红莲教主  阅读(13624)  评论(0编辑  收藏  举报