我正在使用Qt 5.5.0 for Windows。在使用登錄和註冊的對話框中,我使用QVBoxLayout作爲對話框的主要佈局,並將QGridLayout添加到mainLayout。當我點擊「註冊」按鈕時,它會添加更多的LineEdits用於註冊,當我再次單擊它時,這些LineEdit將被刪除。但是,當我移除GridLayout中的小部件時,小部件仍然存在,並且窗口不會調整大小。無法刪除Qt中佈局的子佈局中的小部件
我沒有足夠的聲譽添加圖像,所以我在這裏上傳iamges: http://i.imgbox.com/WAS6KAQw.png
下面是我的一些代碼,所以如何去除網格佈局小部件?
LoginDialog::LoginDialog(MainWindow * mw, AgendaService * as, QWidget * parent)
// Initialization list
{
// Manage layouts and UI
passwordEdit->setEchoMode(QLineEdit::Password);
mainLayout = new QVBoxLayout;
editLayout = new QGridLayout;
bottomLayout = new QVBoxLayout;
editLayout->addWidget(usernameLabel, 0, 0);
editLayout->addWidget(usernameEdit, 0, 1);
editLayout->addWidget(passwordLabel, 1, 0);
editLayout->addWidget(passwordEdit, 1, 1);
mainLayout->addLayout(editLayout);
QHBoxLayout * buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(registerButton);
buttonLayout->addWidget(loginButton);
bottomLayout->addLayout(buttonLayout);
bottomLayout->addWidget(notifyBar);
mainLayout->addLayout(bottomLayout);
setLayout(mainLayout);
// Set default button
registerButton->setDefault(false);
loginButton->setDefault(true);
// Other code
}
void LoginDialog::showRegister()
{
loginButton->setEnabled(false);
editLayout->addWidget(useremailLabel, 2, 0);
editLayout->addWidget(useremailEdit, 2, 1);
editLayout->addWidget(userphoneLabel, 3, 0);
editLayout->addWidget(userphoneEdit, 3, 1);
}
void LoginDialog::hideRegister()
{
editLayout->removeWidget(userphoneEdit);
editLayout->removeWidget(userphoneLabel);
editLayout->removeWidget(useremailEdit);
editLayout->removeWidget(useremailLabel);
loginButton->setEnabled(true);
}
...