Qt代码创建布局
Posted yaked
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt代码创建布局相关的知识,希望对你有一定的参考价值。
代码与布局
创建一个类似Win+R的小窗口
#include <QApplication>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QtWidgets>
#include <QHBoxLayout>
#include <QVBoxLayout>
int main(int argc, char ** argv)
QApplication app(argc, argv);
QLabel *infoLabel=new QLabel;
QLabel * cmdlabel = new QLabel;
QLineEdit * cmdlineEdit = new QLineEdit;
QPushButton *submitButton = new QPushButton;
QPushButton * cancelButton =new QPushButton;
QPushButton * browerButton = new QPushButton;
infoLabel->setText("Please input command in lineEdit");
cmdlabel ->setText("Open");
cmdlineEdit ->clear();
submitButton->setText("Submit");
cancelButton->setText("Cancel");
browerButton->setText("Browser");
QHBoxLayout *cmdlayout = new QHBoxLayout;
cmdlayout->addWidget(cmdlabel);
cmdlayout->addWidget(cmdlineEdit);
QHBoxLayout* buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(submitButton);
buttonLayout->addWidget(cancelButton);
buttonLayout->addWidget(browerButton);
QVBoxLayout* mainLayout=new QVBoxLayout;
mainLayout->addWidget(infoLabel);
mainLayout->addLayout(cmdlayout); // Former created layout.
mainLayout->addLayout(buttonLayout);
// Main window
QWidget *w = new QWidget;
w->setLayout(mainLayout);
w->setWindowTitle("Main window");
w->show();
return app.exec();
首先创建界面上所有的元素,再设属性,布局。
以上是关于Qt代码创建布局的主要内容,如果未能解决你的问题,请参考以下文章