《qgroupbox在界面布局中的使用方法》由会员分享,可在线阅读,更多相关《qgroupbox在界面布局中的使用方法(7页珍藏版)》请在金锄头文库上搜索。
1、此例程主要展示用代码方式创建控件并用 Layout 管理类对其进行布局;例程来自 Qt5.2,如过是默认安装,代码位于:C:QtQt5.2.05.2.0mingw48_32exampleswidgetslayoutsbasiclayouts涉及类:QDialog QPushbotton QGroupBox QLabel QLineEdit QComboBox QTextEdit QHBoxLayout QVBoxLayout QFormLayout QGridLayout QMenu QMenuBarmain.cpp#include #include dialog.hint main(int
2、argc, char *argv) QApplication app(argc, argv);Dialog dialog;dialog.show();return app.exec();dialog.h#ifndef DIALOG_H#define DIALOG_H#include class QAction;class QDialogButtonBox;class QGroupBox;class QLabel;class QLineEdit;class QMenu;class QMenuBar;class QPushButton;class QTextEdit;class Dialog :
3、public QDialogQ_OBJECTpublic:Dialog();private:void createMenu();void createHorizontalGroupBox();void createGridGroupBox();void createFormGroupBox();enum NumGridRows = 3, NumButtons = 4 ;QMenuBar *menuBar;QGroupBox *horizontalGroupBox;QGroupBox *gridGroupBox;QGroupBox *formGroupBox;QTextEdit *smallEd
4、itor;QTextEdit *bigEditor;QLabel *labelsNumGridRows;QLineEdit *lineEditsNumGridRows;QPushButton *buttonsNumButtons;QDialogButtonBox *buttonBox;QMenu *fileMenu;QAction *exitAction;#endif / DIALOG_Hdialog.cpp#include #include dialog.hDialog:Dialog()createMenu(); /创键菜单createHorizontalGroupBox();/创建第一组控
5、件createGridGroupBox(); /创建第二组控件createFormGroupBox(); /创建第三组控件bigEditor = new QTextEdit; /创建一个独立的文本编辑框bigEditor-setPlainText(tr(This widget takes up all the remaining space in the top-level layout.);/创建对话框按钮组buttonBox = new QDialogButtonBox(QDialogButtonBox:Ok| QDialogButtonBox:Cancel);connect(button
6、Box, SIGNAL(accepted(), this, SLOT(accept(); /连接按钮信号connect(buttonBox, SIGNAL(rejected(), this, SLOT(reject();QVBoxLayout *mainLayout = new QVBoxLayout; /创建垂直布局对象mainLayout-setMenuBar(menuBar); /加入菜单栏mainLayout-addWidget(horizontalGroupBox); /加入第一组控件mainLayout-addWidget(gridGroupBox); /加入第二组控件mainLa
7、yout-addWidget(formGroupBox); /加入第三组控件mainLayout-addWidget(bigEditor); /加入文本编辑框mainLayout-addWidget(buttonBox); /加入按钮setLayout(mainLayout); /布局加入 app 对象setWindowTitle(tr(Basic Layouts); /设置窗口标题/创建菜单栏void Dialog:createMenu()menuBar = new QMenuBar; /Dialog 类没有菜单栏,所以创建一个fileMenu = new QMenu(tr( /创建一个菜单
8、exitAction = fileMenu-addAction(tr(E/添加一个菜单选项menuBar-addMenu(fileMenu); /菜单加入菜单栏里connect(exitAction, SIGNAL(triggered(), this, SLOT(accept();/连接菜单信号/加入第一组控件void Dialog:createHorizontalGroupBox()horizontalGroupBox = new QGroupBox(tr(Horizontal layout); /创建组控件QHBoxLayout *layout = new QHBoxLayout; /创建
9、水平布局对象for (int i = 0; i addWidget(buttonsi); /加入布局horizontalGroupBox-setLayout(layout); /将布局好的按钮添加套组控件里/加入第二组控件void Dialog:createGridGroupBox()gridGroupBox = new QGroupBox(tr(Grid layout); /创建组控件QGridLayout *layout = new QGridLayout; /创建网格布局对象for (int i = 0; i addWidget(labelsi, i + 1, 0); /将建标签控件放在
10、 i+1 行,0 列位置layout-addWidget(lineEditsi, i + 1, 1); /将建标编辑框控件放在 i+1 行,1 列位置smallEditor = new QTextEdit; /创建一个文本编辑控件smallEditor-setPlainText(tr(This widget takes up about two thirds of the grid layout.);layout-addWidget(smallEditor, 0, 2, 4, 1); /将文本编辑控件放在0 行,2 列位置,行跨度为 4 行,列跨读为 1 列layout-setColumnSt
11、retch(1, 10);/将第 1 列的控件(行编辑框控件)拉伸系数为 10layout-setColumnStretch(2, 20);/将第 2 列的控件(文本编辑控件)拉伸系数为 20gridGroupBox-setLayout(layout);/加入第三组控件void Dialog:createFormGroupBox()formGroupBox = new QGroupBox(tr(Form layout);/创建组控件QFormLayout *layout = new QFormLayout; /创建表格式布局对象layout-addRow(new QLabel(tr(Line 1:), new QLineEdit); /添加第一个标签和行编辑器layout-addRow(new QLabel(tr(Line 2, long text:), new QComboBox);/添加第二个标签和行编辑器layout-addRow(new QLabel(tr(Line 3:), new QSpinBox);/添加第三个标签和行编辑器formGroupBox-setLayout(layout);