读Qt Demo——Basic Layouts Example

此例程主要展示用代码方式创建控件并用Layout管理类对其进行布局;

例程来自Qt5.2,如过是默认安装,代码位于:C:\Qt\Qt5.2.0\5.2.0\mingw48_32\examples\widgets\layouts\basiclayouts

涉及类:QDialog QPushbotton QGroupBox QLabel QLineEdit QComboBox QTextEdit QHBoxLayout QVBoxLayout QFormLayout QGridLayout QMenu QMenuBar

image

 

main.cpp

#include <QApplication>

#include "dialog.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Dialog dialog;
    dialog.show();

    return app.exec();
}

 

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

class QAction;
class QDialogButtonBox;
class QGroupBox;
class QLabel;
class QLineEdit;
class QMenu;
class QMenuBar;
class QPushButton;
class QTextEdit;

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog();

private:
    void createMenu();
    void createHorizontalGroupBox();
    void createGridGroupBox();
    void createFormGroupBox();

    enum { NumGridRows = 3, NumButtons = 4 };

    QMenuBar *menuBar;
    QGroupBox *horizontalGroupBox;
    QGroupBox *gridGroupBox;
    QGroupBox *formGroupBox;
    QTextEdit *smallEditor;
    QTextEdit *bigEditor;
    QLabel *labels[NumGridRows];
    QLineEdit *lineEdits[NumGridRows];
    QPushButton *buttons[NumButtons];
    QDialogButtonBox *buttonBox;

    QMenu *fileMenu;
    QAction *exitAction;
};

#endif // DIALOG_H

 

 

dialog.cpp

#include <QtWidgets>

#include "dialog.h"

Dialog::Dialog()
{
    createMenu();              //创键菜单
    createHorizontalGroupBox();//创建第一组控件
    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(buttonBox, 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);        //加入第二组控件
    mainLayout->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("&File"), this); //创建一个菜单
    exitAction = fileMenu->addAction(tr("E&xit"));//添加一个菜单选项
    menuBar->addMenu(fileMenu); //菜单加入菜单栏里

    connect(exitAction, SIGNAL(triggered()), this, SLOT(accept()));//连接菜单信号
}

//加入第一组控件
void Dialog::createHorizontalGroupBox()
{
    horizontalGroupBox = new QGroupBox(tr("Horizontal layout"));  //创建组控件
    QHBoxLayout *layout = new QHBoxLayout;                        //创建水平布局对象

    for (int i = 0; i < NumButtons; ++i) {                        //创建按钮
        buttons[i] = new QPushButton(tr("Button %1").arg(i + 1));
        layout->addWidget(buttons[i]);                            //加入布局
    }
    horizontalGroupBox->setLayout(layout);                        //将布局好的按钮添加套组控件里
}

//加入第二组控件
void Dialog::createGridGroupBox()
{
    gridGroupBox = new QGroupBox(tr("Grid layout"));              //创建组控件
    QGridLayout *layout = new QGridLayout;                        //创建网格布局对象

    for (int i = 0; i < NumGridRows; ++i) {
        labels[i] = new QLabel(tr("Line %1:").arg(i + 1));        //创建标签控件
        lineEdits[i] = new QLineEdit;                             //创建行编辑框控件
        layout->addWidget(labels[i], i + 1, 0);                   //将建标签控件放在i+1行,0列位置
        layout->addWidget(lineEdits[i], 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->setColumnStretch(1, 10);//将第1列的控件(行编辑框控件)拉伸系数为10
    layout->setColumnStretch(2, 20);//将第2列的控件(文本编辑控件)拉伸系数为20
    gridGroupBox->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);
}

posted on 2014-01-22 12:01  Worldsing  阅读(589)  评论(0编辑  收藏  举报