Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Show a modal QDialog just after MainWindow appears
Forum Updated to NodeBB v4.3 + New Features

[Solved] Show a modal QDialog just after MainWindow appears

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 0 Offline
    0 Offline
    0l1v1er
    wrote on last edited by
    #1

    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]

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

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

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • 0 Offline
        0 Offline
        0l1v1er
        wrote on last edited by
        #3

        [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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          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()));
          }
          @

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          1
          • Z Offline
            Z Offline
            zeljko
            wrote on last edited by
            #5

            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).

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved