问题
In my Qt application, I have a QMainWindow
subclass for the UI (ProgramWindow
) and a QDialog
subclass (SettingsDialog
) which is shown when the user clicks the "Settings" button in the main window. I'm using the following code to implement this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ProgramWindow pw;
pw.show();
pw.setFixedSize(pw.size());
SettingsDialog set(&pw);
QObject::connect(&pw, SIGNAL(settingsButtonClicked()), &set, SLOT(show()));
return a.exec();
}
However, I have noticed that when the settings dialog is shown for the second time (the user clicks the settings button, the dialog is shown, then the user closes it, then clicks on "Settings" again) the window is not shown "smoothly", as other windows are in Windows 8.1's default theme, it just suddenly appears on the screen like in the old XP theme, for example. Note that this doesn't happen the first time the window is shown.
What could be the reason?
EDIT: I suspect that instead of destroying the window, close()
called on a QDialog will end up calling Win32's ShowWindow
with the SW_HIDE
parameter, and so when it's shown again later, the Windows UI manager doesn't see it as creating the window but merely re-showing it, so it doesn't "play" the animation. The solution is to create the dialog using new
and letting Qt dispose of it when appropriate:
MyDialog* dialog = new MyDialog;
dialog->show();
// in MyDialog's constructor
setAttribute(Qt::WA_DeleteOnClose);
来源:https://stackoverflow.com/questions/32931803/why-does-opening-a-qdialog-the-second-time-cause-it-to-be-shown-immediately-not