qt-C++笔记之组件-分组框QGroupBox
code review!
文章目录
- qt-C++笔记之组件-分组框QGroupBox
- 1.《Qt 6 C++开发指南》p75
- 2.《Qt 官方文档》
- 3.《Qt 5.12实战》——5.9 分组框控件
1.《Qt 6 C++开发指南》p75
2.《Qt 官方文档》
中间段落翻译:
我把示例补充完整:
#include <QApplication>
#include <QWidget>
#include <QGroupBox>
#include <QRadioButton>
#include <QVBoxLayout>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("Radio Buttons Example");
QGroupBox *groupBox = new QGroupBox(QObject::tr("Exclusive Radio Buttons"), &window);
QRadioButton *radio1 = new QRadioButton(QObject::tr("&Radio button 1"), &window);
QRadioButton *radio2 = new QRadioButton(QObject::tr("R&adio button 2"), &window);
QRadioButton *radio3 = new QRadioButton(QObject::tr("Ra&dio button 3"), &window);
radio1->setChecked(true);
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(radio1);
vbox->addWidget(radio2);
vbox->addWidget(radio3);
vbox->addStretch(1);
groupBox->setLayout(vbox);
QVBoxLayout *layout = new QVBoxLayout(&window);
layout->addWidget(groupBox);
window.setLayout(layout);
window.show();
return app.exec();
}
组件关系
+-------------------------------------------------------+
| QWidget (window) |
| |
| +-----------------------------------------------+ |
| | QVBoxLayout (layout) | |
| | | |
| | +---------------------------------------+ | |
| | | QGroupBox (groupBox) | | |
| | | | | |
| | | +-------------------------------+ | | |
| | | | QVBoxLayout (vbox) | | | |
| | | | | | | | |
| | | | +----------------+ | | | | |
| | | | | QRadioButton | | | | | |
| | | | | (radio1) | | | | | |
| | | | +----------------+ | | | | |
| | | | +----------------+ | | | | |
| | | | | QRadioButton | | | | | |
| | | | | (radio2) | | | | | |
| | | | +----------------+ | | | | |
| | | | +----------------+ | | | | |
| | | | | QRadioButton | | | | | |
| | | | | (radio3) | | | | | |
| | | | +----------------+ | | | | |
| | | | | | | | |
| | | | addStretch(1) | | | | |
| | | +-------------------------------+ | | |
| | | | | |
| +-----------------------------------------------+ |
| |
+-------------------------------------------------------+