我正在使用 Windows 版 Qt 5.5.0。在用于登录和注册的对话框中,我使用 QVBoxLayout 作为对话框的主布局,并将 QGridLayout 添加到 mainLayout。当我单击“注册”按钮时,它将添加太多用于注册的 LineEdit,当我再次单击它时,这些 LineEdit 将被删除。但是,当我删除 GridLayout 中的小部件时,小部件仍然存在,并且窗口不会调整大小。
我没有足够的声誉来添加图像,因此我在此处上传图像:http://i.imgbox.com/WAS6KAQw.png 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);
}
...