Please nominate your Qt Champions for 2021!

Hide a QDialog

  • I subclass a QDialog and sometimes I would like to hide the QDialog within the constructor of my subclass, and show it again on an event.
    However it looks like it's not possible to hide it from the construction, could someone confirm this?
    What can of workaround do I have, some tricks with a singleshot timer?

  • If you create a dialog(specially subclass of QDialog), it won't show until the show() or exec() called.

    Yes, you can use timer to handle this: hide for seconds, then show it :)

  • Hum right. Let's say that I want the dialog to "start" hidden, and the either close it (so the user will never see it) or show it. Is there a way to do this?

  • Simply create an instance, and don't call show() on it?

  • Where is the code that "controls" the display behavior?

  • For dialog, if you want hide or delete it, you can just close it and delete.

    Do you want a floating widget, which display like a balloon tooltip, show some seconds then disappeared?

  • MyDialog.h
    #include <QDialog>
    class MyDialog : public QDialog
    explicit MyDialog(QWidget *parent = 0);

    MyDialog::MyDialog(QWidget *parent) : QDialog(parent){

    You can "start dialog hidden":
    @dialog = new MyDialog();@
    Dialog is present, but hidden.
    You can close it:
    You can delete it:
    @delete dialog;
    dialog = 0;@
    You can show it:

  • In fact my dialog checks for software updates: it shows the software version, then opens a distant xml file and displays a download button if a newer version exists.
    This works perfectly, but now I would like to add an option to silently check for update and show the dialog only if a newer version exists.

    [quote author="Volker" date="1306836130"]Where is the code that "controls" the display behavior?[/quote] Partly in the constructor and partly in the slots connected to a QNetworkReply object.

    Right now everything is done in the constructor. I like it because from my main I can call exec wihch is blocking, and everything is done in the subclass

    If I don't call exec, my UpdatesDialog will be immediatly destroyed. To avoid this, I need a pointer to it, then to connect it to slot of my mainwindow, etc.. @void MainWindow::slotCheckForUpdatesClicked()
    UpdatesDialog dialog(GLOBAL_MAJOR, GLOBAL_MINOR, GLOBAL_BUILD, GLOBAL_ARCH64, true, this);
    } @

  • Then move the check out of the dialog into your main application or something else and open the dialog only if there is an update.

  • I solved it by optionally calling exec from the constructor.
    Thanks everybody!

  • This makes your constructor to not finish (read your constructor returns only after your dialog has been executed) before your dialog is dismissed.

    If you ever happen to subclass your dialog, you will work on an not completely constructed object and you're most likely to crash your app!


    You have been showed a way to do it properly.

  • Oops, ok I'll change this right now!

Log in to reply