我正在使用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);
}
...