QT之Layout

#include 
#include 
#include 
#include 
#include 
#include 
#include 
int main(int argc, char* argv[])
{
    /*构造一个应用程序*/
    QApplication app(argc,argv);

    /*构造一个窗口*/
    QWidget window;
#if 0
    /*构造一个按钮*/
    QPushButton button;

    /*构造一个输入框*/
    QLineEdit lineEdit;

    /*构造一个水平布局*/
    QHBoxLayout layout;

    /*设置按钮的文本*/
    button.setText("Hello");

    /*将按钮加入到布局中*/
    layout.addWidget(&button);

    /*设置间距*/
    layout.addSpacing(50);

     /*将输入框加入到布局中*/
    layout.addWidget(&lineEdit);

    /*设置布局中的对象不会随着窗口大小的改变而改变*/
    layout.addStretch(1);
#endif

#if 0
    /*构造网格布局*/
    QGridLayout layout;
    /*使展示的网格内容居中*/
    layout.setRowStretch(0,1);
    layout.setRowStretch(4,1);
    layout.setColumnStretch(0,1);
    layout.setColumnStretch(3,1);

    /*为布局添加窗体对象*/
    layout.addWidget(&button,1,1);
    layout.addWidget(&lineEdit,1,2);
    layout.addWidget(new QPushButton("World"),2,1);
    layout.addWidget(new QLineEdit(),2,2);
    layout.addWidget(new QPushButton("Welcome"),3,1,1,2);
#endif
    QGridLayout layout;
    layout.setRowStretch(0,1);
    layout.setRowStretch(4,1);
    layout.setColumnStretch(0,1);
    layout.setColumnStretch(3,1);

    QLineEdit *password;
    layout.addWidget(new QLabel("Username:"),1,1);
    layout.addWidget(new QLineEdit(),1,2);
    layout.addWidget(new QLabel("Password:"),2,1);
    layout.addWidget(password = new QLineEdit(),2,2);
    /*设置输入框为密码显示方式*/
    password->setEchoMode(QLineEdit::Password);

    QHBoxLayout *hlayout;
    /*布局嵌套*/
    layout.addLayout(hlayout = new QHBoxLayout,3,2);
    hlayout->addStretch(1);
    hlayout->addWidget(new QPushButton("Login"));

    /*设置窗口标题*/
    window.setWindowTitle("Layout");

    /*设置布局*/
    window.setLayout(&layout);

    /*显示窗口*/
    window.show();

    /*执行应用程序并返回*/
    return app.exec();
}

你可能感兴趣的:(QT)