[SOLVED] QDialog hide(), exec() and show() resulting in unexpected behavior
-
Dear fellow programmers,
I want to do the following:
- hide the current dialog
- exec a new dialog (that is: showing a new dialog modally)
- show the current dialog again
From the bugreport https://bugreports.qt-project.org/browse/QTBUG-3657 I learned that hiding (QDialog::Hide) a dialog causes QDialog::exec to return, resulting in -for me- unexpected behavior .
Especially this code seems natural to me, yet the bug report stated it is intended to always close the current dialog (SecondDialog), instead of showing it:
@
void SecondDialog::on_button_goto_third_clicked()
{
ThirdDialog d;
this->hide();
d.exec();
this->show();
}
@(see http://richelbilderbeek.nl/CppExerciseQtHideAndShow2.htm for full code and project download)
How do I work around this intended behavior?
Thanks for your help, Bilderbikkel
-
if d.exec() is modal it will be placed on the parent and hold all inputs from the second dialog. So, don't hide it at all and let the third dialog be put above the second one?
Maybe an other option is to run the third dialog in a different thread and wait in the second dialog until it is finished? Never tried it before, just an idea.
Greetz -
@Jeroentje:Thanks for your suggestion. Yet, I know exec() blocks input to the parent windows, but I just not to see those parent windows it at all...
-
I thought you might say that ;-)
If I have a great idea I'll let you know.
Let me know if you fixed it, might need it myself in the future. -
If I understand correctly, you want the second dialog to show a third one, but the second dialog is supposed to be hidden, while the third one is showing. Can't you just close() the second dialog and let the primary one (Main window?) show the third dialog? And then, after the third one has closed, just show the second one again.
@void MainDialog::foo()
{
for(;;)
{
SecondDialog d1;
int ret = d1.exec();
if(ret == MAGIC_NUMBER)
{
ThirdDialog d2;
d2.exec();
}
else
{
break;
}
}
}@Note: The second dialog returns a special value to indicate we will show the third dialog now.
-
@MuldeR: thanks for your suggestion, but it wouldn't scale: for example, if I would like to put a dialog before the first one again.
I did find a solution and posted it here:
http://richelbilderbeek.nl/CppExerciseQtHideAndShow2Answer.htm
It involves adding two boolean member variables, two signals and two slots and calling QDialog::exec from a while loop.
@utcenter: would you agree to prefer this over implementing a new widget?
Cheers, Bilderbikkel
-
To follow utcenter's advice, I did implement a new widget. See http://richelbilderbeek.nl/CppExerciseQtHideAndShow3Answer.htm for the QtHideAndShowDialog class.