建议每次显示时都使用show()
/exec()
而不是动态创建对话框。hide()
也使用QDialog
代替QWidget
.
在主窗口的构造函数中创建并隐藏它
MainWindow::MainWindow()
{
// myDialog is class member. No need to delete it in the destructor
// since Qt will handle its deletion when its parent (MainWindow)
// gets destroyed.
myDialog = new SomeDialog(this);
myDialog->hide();
// connect the accepted signal with a slot that will update values in main window
// when the user presses the Ok button of the dialog
connect (myDialog, SIGNAL(accepted()), this, SLOT(myDialogAccepted()));
// remaining constructor code
}
在连接到按钮clicked()
事件的插槽中简单地显示它,并在必要时将一些数据传递给对话框
void myClickedSlot()
{
myDialog->setData(data);
myDialog->show();
}
void myDialogAccepted()
{
// Get values from the dialog when it closes
}