我在Qt上有这样的代码。
wdgCenter = new QWidget(wdgMain);
wdgCenter->move(wdgCenter->parentWidget()->geometry().center());
wdgCenter->resize(QSize(170, 115));
welcomeLbl = new QLabel("Welcome to the Notes. Log in first.", wdgCenter);
welcomeLbl->setGeometry(QRect(QPoint(0, 0), QSize(175, 20)));
loginLbl = new QLabel("Login", wdgCenter);
loginLbl->setGeometry(QRect(QPoint(0, 30), QSize(50, 20)));
pswdLbl = new QLabel("Password", wdgCenter);
pswdLbl->setGeometry(QRect(QPoint(0, 60), QSize(50, 20)));
loginLine = new QLineEdit (wdgCenter);
loginLine->setGeometry(QRect(QPoint(60, 30), QSize(110, 20)));
passwordLine = new QLineEdit (wdgCenter);
passwordLine->setGeometry(QRect(QPoint(60, 60), QSize(110, 20)));
logInBtn = new QPushButton ("Log In", wdgCenter);
logInBtn->setGeometry(QRect(QPoint(0, 90), QSize(50, 25)));
createBtn = new QPushButton ("Create", wdgCenter);
createBtn->setGeometry(QRect(QPoint(60, 90), QSize(50, 25)));
您可以在下面的第一张图片中看到结果。
我想将wdgCenter
移到窗口的中心。我在下面试过QGridLayout
。
QGridLayout* gLayout = new QGridLayout(wdgMain);
gLayout->addWidget( welcomeLbl );
gLayout->setAlignment(welcomeLbl,Qt::AlignHCenter);
gLayout->addWidget( loginLbl, 1, 0 );
gLayout->setAlignment(loginLbl,Qt::AlignHCenter);
gLayout->addWidget( pswdLbl, 2, 0 );
gLayout->setAlignment(pswdLbl,Qt::AlignHCenter);
gLayout->addWidget( loginLine, 1, 1 );
gLayout->setAlignment(loginLine,Qt::AlignHCenter);
gLayout->addWidget( passwordLine, 2, 1 );
gLayout->setAlignment(passwordLine,Qt::AlignHCenter);
gLayout->addWidget( logInBtn, 3, 0 );
gLayout->setAlignment(logInBtn,Qt::AlignHCenter);
gLayout->addWidget( createBtn, 3, 1 );
gLayout->setAlignment(createBtn,Qt::AlignHCenter);
结果你可以在下面的下一张图片中看到。
我还尝试了QHBoxLayout
和QHBoxLayout
,但他们只是在行或列中绘制了我的对象。
在Qt Designer中,我使用这样的垂直和水平间隔器来做到这一点:
但我需要以编程方式执行此操作。
如何将wdgCenter
设为中心?