横版和竖版布局

执行结果

源码
//程序抽象类
#include <QApplication>
//窗口类
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
//自动竖版布局
#include <QVBoxLayout>
//自动横版布局
#include <QHBoxLayout>
//自动格子布局
#include <QGridLayout>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QWidget w;
//按钮也是一个窗口
QPushButton button;
button.setText("你好");
//button.setParent(&w);
button.show();
//输入框也是一个窗口
QLineEdit editor;
editor.setEchoMode(QLineEdit::Normal);
editor.setPlaceholderText("请输入文本");
//editor.setParent(&w);
editor.show();
//水平方向上的layout
//QVBoxLayout vBox;
//vBox.addWidget(&button);
//vBox.addWidget(&editor);
//垂直方向上的layout
QHBoxLayout hBox;
//设置左右弹簧 使得控件大小不随窗口改变
hBox.addStretch(1);
hBox.addWidget(&button,1);
//设置两个空间的间距为20
hBox.addSpacing(20);
hBox.addWidget(&editor,1);
hBox.addStretch(1);
w.setWindowTitle("hello world");
w.show();
w.setLayout(&hBox);
return app.exec();
}
网友评论