QGroupBox窗口部件提供了一個有標題的組合框
組合框提供一個框架、一個標題和一個鍵盤快捷鍵,而且顯示在它裏面地其它不一樣窗口部件。標題在上面,鍵盤快捷鍵移動鍵盤焦點到組合框的一個子窗口部件,而且子窗口部件將會排列在這個框架中地一個數組中web
使用它的最簡單方式是建立一個爲你所想要的列數(或者行數)和方向的組合框,而且而後建立其它窗口部件時,把這個組合框做爲父對象數組
然而,也能夠在構造函數以後改變方向(orientation())和列數(或者行數,columns()),或者忽略全部自動佈局支持而且本身來管理全部的這一切。你也能夠經過addSpace()把「空白」添加到組合框中框架
QGroupBox也讓你設置標題(title(),一般在構造函數中設置)和標題對齊方式(alignment())svg
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
QGridLayout *grid = new QGridLayout;
grid->addWidget(createFirstExclusiveGroup(),0,0);
grid->addWidget(createSecondExclusiveGroup(),0,1);
grid->addWidget(createNonExclusiveGroup(),1,0);
QWidget *widget = new QWidget;
widget->setLayout(grid);
widget->resize(480, 320);
widget->show();
}
QGroupBox *Widget::createFirstExclusiveGroup()
{
QGroupBox *groupBox = new QGroupBox(tr("Exclusive Radio Buttons"));
QRadioButton *radio1 = new QRadioButton(tr("&Radio button 1"));
QRadioButton *radio2 = new QRadioButton(tr("R&adio button 2"));
QRadioButton *radio3 = new QRadioButton(tr("Ra&dio button 3"));
radio1->setChecked(true);
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(radio1);
vbox->addWidget(radio2);
vbox->addWidget(radio3);
vbox->addStretch(1);
groupBox->setLayout(vbox);
return groupBox;
}
QGroupBox *Widget::createSecondExclusiveGroup()
{
QGroupBox *groupBox = new QGroupBox(tr("E&xclusive Radio Buttons"));
groupBox->setCheckable(true);
groupBox->setChecked(true);
QRadioButton *radio1 = new QRadioButton(tr("Rad&io button1"));
QRadioButton *radio2 = new QRadioButton(tr("Radi&o buttoon2"));
QRadioButton *radio3 = new QRadioButton(tr("Radio &button3"));
radio1->setChecked(true);
QCheckBox *checkBox = new QCheckBox(tr("Ind&ependent checkbox"));
checkBox->setChecked(true);
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(radio1);
vbox->addWidget(radio2);
vbox->addWidget(radio3);
vbox->addWidget(checkBox);
groupBox->setLayout(vbox);
return groupBox;
}
QGroupBox *Widget::createNonExclusiveGroup()
{
QGroupBox *groupBox = new QGroupBox(tr("No-Exclusive Checkboxes"));
groupBox->setFlat(true);
QCheckBox *checkBox1 = new QCheckBox(tr("&Checkbox 1"));
QCheckBox *checkBox2 = new QCheckBox(tr("C&heckbox 2"));
checkBox2->setChecked(true);
QCheckBox *tristateBox = new QCheckBox(tr("Tri-&state buttton"));
tristateBox->setTristate(true);//是否啓用三種狀態切換
tristateBox->setCheckState(Qt::PartiallyChecked);//介於Qt::Unchecked和Qt::Checked之間
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(checkBox1);
vbox->addWidget(checkBox2);
vbox->addWidget(tristateBox);
vbox->addStretch(1);
groupBox->setLayout(vbox);
return groupBox;
}