|
1Qter豆
先声明一下,本人新人一个,非计算机行业,算是跨行爱好,所以编程不在行,纯属个人喜好,想学学QT,免得被笑后面的问题无知……
问题是:我看了论坛的QT快速入门教程第三章,用纯代码写界面,之前又看过一些豆子的教程,就顺便写了Layout布局,然后我就蒙了,我记得要来个setCentalWidget(mainLayout)的嘛,然后报错,想起好像这是QDialog不是QWidget……
没办法我就删了,直接运行,就出来了我要的登录界面,我愣了。
那么我的疑问来了,我写了这么多Layout,QT怎么知道我只要显示mainLayout?layout1/2/3/4都自动隐藏了?
新人求解,还望指教,谢谢!
- #include "logindlg.h"
- #include <QHBoxLayout>
- #include <QVBoxLayout>
- LoginDLG::LoginDLG(QWidget *parent):QDialog(parent)
- {
- usrLabel = new QLabel(tr("UserName"));
- pwdLabel = new QLabel(tr("Password"));
- usrLineEdit = new QLineEdit();
- usrLineEdit->setPlaceholderText(tr("Username"));
- pwdLineEdit = new QLineEdit();
- pwdLineEdit->setPlaceholderText(tr("Password"));
- loginBtn = new QPushButton(tr("Login"));
- exitBtn = new QPushButton(tr("Exit"));
- //set the label layout
- QVBoxLayout *layout1 = new QVBoxLayout();
- layout1->addWidget(usrLabel);
- layout1->addWidget(pwdLabel);
- //set the lineEdit layout
- QVBoxLayout *layout2 = new QVBoxLayout();
- layout2->addWidget(usrLineEdit);
- layout2->addWidget(pwdLineEdit);
- //set the Label and LineEdit together
- QHBoxLayout *layout3 = new QHBoxLayout();
- layout3->addLayout(layout1);
- layout3->addLayout(layout2);
- //set the button layout
- QHBoxLayout *layout4 = new QHBoxLayout();
- layout4->addStretch();
- layout4->addWidget(loginBtn);
- layout4->addStretch();
- layout4->addWidget(exitBtn);
- layout4->addStretch();
- //Set Main layout
- QVBoxLayout *mainLayout = new QVBoxLayout(this);
- mainLayout->addLayout(layout3);
- mainLayout->addLayout(layout4);
- connect(loginBtn, &QPushButton::clicked, this, &LoginDLG::accept);
- connect(exitBtn, &QPushButton::clicked, this, &LoginDLG::close);
- }
- LoginDLG::~LoginDLG()
- {
- }
复制代码
|
最佳答案
查看完整内容
应该是这句把mainLayout设为了这个dialog顶级layout
参见QLayoutayout(QWidget *parent)
|