[Solved] Show a modal QDialog just after MainWindow appears



  • Hi,

    I'm a newbie on QT. I try to build a QT Desktop application with QT 5.3.

    I need to open a Modal Dialog juste after MainWindow appears (login dialog).

    I try this:
    @
    void MainWindow::showEvent(QShowEvent*)
    {
    LoginDialog *login = new LoginDialog(this);
    login->setModal(true);
    if (login->exec() != QDialog::Accepted)
    close();
    }
    @
    But I have two problem:
    1/ The dialog is show before the MainWindow
    2/ When the login dialog don't return "Accepted" the application hang...

    Do you have any idea?

    Thanks for your help.

    Thanks,
    Olivier

    [andreyc EDIT: added @ around code]



  • Hi,

    quick solution could be using showEvent to start a Timer (for example 100ms) and open the login page in connected slot



  • [quote author="mcosta" date="1401291075"]Hi,

    quick solution could be using showEvent to start a Timer (for example 100ms) and open the login page in connected slot[/quote]

    I thought about it but I thought there was a "cleaner" solution.

    btw it works.

    Thanks.

    Olivier



  • Hi,

    a simpler solution could be start a single shot timer in Window constructor with interval set to 0. In this way the slot will be processed after all pending events (including showEvent())

    @
    MainWindow::MainWindow(QWidget* w) ...
    {
    ....
    QTimer::singleShot(0, this, SLOT(openLoginDialog()));
    }
    @



  • QTimer::singleShot() is best solution definitelly. I'm using that whenever I need to execute something right after current event.
    Also, you can create your own custom event and pass it to the widget, so you'll get almost same result. It is important that in such solutions there's no need for sleep or timeouts, it just flows and never makes troubles (of course if you know what you're doing).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.