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. How to wait in a slot?
Forum Update on Monday, May 27th 2025

How to wait in a slot?

Scheduled Pinned Locked Moved Solved General and Desktop
event loopsignal & slot
11 Posts 3 Posters 4.9k Views
  • 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.
  • T Offline
    T Offline
    tmp15711
    wrote on 17 Nov 2017, 14:10 last edited by tmp15711
    #1

    I want to show a dialog in a slot, and at most one dialog should be shown at any time. Sample code:

    struct Test: QObject{
        Q_OBJECT
    public slots:
        void showDialog(){
            while(dialogShown) qApp->processEvents();
    
            dialogShown = true;
            QMessageBox::information(nullptr, "info", "dialog shown");
            dialogShown = false;
        }
    
    private:
        bool dialogShown = false;
    };
    

    However, this would not work since showDialog will block itself when called more than once.
    How can I achieve expected behavior correctly?

    Edit: Is there any problem with the following code?

    struct Test: QObject{
        Q_OBJECT
    public slots:
        void showDialog(){
            while(dialogShown) qApp->processEvents();
            dialogShown = true;
    
            QMessageBox box(QMessageBox::Information, "info", "dialog shown");
            QObject::connect(&box, &QMessageBox::finished, this, [&]{ dialogShown = false; });
            box.exec();
        }
    
    private:
        bool dialogShown = false;
    };
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 17 Nov 2017, 14:22 last edited by
      #2

      Hi,

      You should rather use something like QDialog::open and connect a slot to the finished signal and change your variable there so there's no need for any while loop, you just check the value of the dialogShown. Note that the usual multithreading protection consideration should be taken.

      There might be other ways but it alls depends on how that dialog is supposed to be used.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • T Offline
        T Offline
        tmp15711
        wrote on 17 Nov 2017, 14:37 last edited by
        #3

        You should rather use something like QDialog::open and connect a slot to the finished signal

        I learned that it is better than exec. But it seems not to help control the number of dialogs shown.

        multithreading protection consideration should be taken

        I do not quite understand. If you mean use mutiple threads, it certainly works. But I wonder is it really necessary?

        A 1 Reply Last reply 17 Nov 2017, 18:23
        0
        • T tmp15711
          17 Nov 2017, 14:37

          You should rather use something like QDialog::open and connect a slot to the finished signal

          I learned that it is better than exec. But it seems not to help control the number of dialogs shown.

          multithreading protection consideration should be taken

          I do not quite understand. If you mean use mutiple threads, it certainly works. But I wonder is it really necessary?

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 17 Nov 2017, 18:23 last edited by
          #4

          @tmp15711 said in How to wait in a slot?:

          multithreading protection consideration should be taken

          I do not quite understand. If you mean use mutiple threads, it certainly works. But I wonder is it really necessary?

          no, he means if you use multiple threads, you need to protect your variables (mutex &co.)

          regarding your problem: set a flag when showing the dialog, clear it on dialog close. if flag is set, don't show other dialog.

          Qt has to stay free or it will die.

          T 1 Reply Last reply 18 Nov 2017, 02:08
          3
          • A aha_1980
            17 Nov 2017, 18:23

            @tmp15711 said in How to wait in a slot?:

            multithreading protection consideration should be taken

            I do not quite understand. If you mean use mutiple threads, it certainly works. But I wonder is it really necessary?

            no, he means if you use multiple threads, you need to protect your variables (mutex &co.)

            regarding your problem: set a flag when showing the dialog, clear it on dialog close. if flag is set, don't show other dialog.

            T Offline
            T Offline
            tmp15711
            wrote on 18 Nov 2017, 02:08 last edited by
            #5

            @aha_1980 I agree. Then, what is the proper way not to show other dialog? Return immediately? If so, showDialog will be tryied again and again to ensure a dialog is shown.

            A 1 Reply Last reply 18 Nov 2017, 07:33
            0
            • T tmp15711
              18 Nov 2017, 02:08

              @aha_1980 I agree. Then, what is the proper way not to show other dialog? Return immediately? If so, showDialog will be tryied again and again to ensure a dialog is shown.

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 18 Nov 2017, 07:33 last edited by aha_1980
              #6

              @tmp15711 said in How to wait in a slot?:

              @aha_1980 I agree. Then, what is the proper way not to show other dialog? Return immediately?

              from what we know about your task so far, yes.

              If so, showDialog will be tryied again and again to ensure a dialog is shown.

              then the question arises why the slot must be called again and again. which signal is it connected to?
              (a possibility would also be to disconnect the slot from the signal when the dialog is shown and connect again after finishing)

              But maybe you could explain what you really want to do. maybe there is a simpler and less "hacky" solution.

              Qt has to stay free or it will die.

              T 1 Reply Last reply 18 Nov 2017, 14:24
              1
              • A aha_1980
                18 Nov 2017, 07:33

                @tmp15711 said in How to wait in a slot?:

                @aha_1980 I agree. Then, what is the proper way not to show other dialog? Return immediately?

                from what we know about your task so far, yes.

                If so, showDialog will be tryied again and again to ensure a dialog is shown.

                then the question arises why the slot must be called again and again. which signal is it connected to?
                (a possibility would also be to disconnect the slot from the signal when the dialog is shown and connect again after finishing)

                But maybe you could explain what you really want to do. maybe there is a simpler and less "hacky" solution.

                T Offline
                T Offline
                tmp15711
                wrote on 18 Nov 2017, 14:24 last edited by tmp15711
                #7

                @aha_1980 The motivation is quite simple. My program will listen to some event, and then do something. But before that, I must get a confirmation from the user. At the same time, I want confirmations appear one by one. Any suggestions?

                A 1 Reply Last reply 18 Nov 2017, 14:41
                0
                • T tmp15711
                  18 Nov 2017, 14:24

                  @aha_1980 The motivation is quite simple. My program will listen to some event, and then do something. But before that, I must get a confirmation from the user. At the same time, I want confirmations appear one by one. Any suggestions?

                  A Offline
                  A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 18 Nov 2017, 14:41 last edited by aha_1980
                  #8

                  @tmp15711 technically you can use a fifo e.g. QQueue. append every time you need to show a dialog but there is currently one open. when a dialog is closed, check if the fifo contains one more element. if yes, show the next dialog, otherwise stop.

                  but from usuability, this sounds horrible. the user is degraded to confirm a bunch of dialogs. I hate every program that acts this way.

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  3
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 18 Nov 2017, 23:14 last edited by
                    #9

                    Is it only a yes/no question that you ask your user ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    T 1 Reply Last reply 19 Nov 2017, 03:40
                    0
                    • S SGaist
                      18 Nov 2017, 23:14

                      Is it only a yes/no question that you ask your user ?

                      T Offline
                      T Offline
                      tmp15711
                      wrote on 19 Nov 2017, 03:40 last edited by
                      #10

                      @SGaist Almost! And?

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 19 Nov 2017, 22:22 last edited by
                        #11

                        And then you should maybe consider creating a widget to handle your threads requests for input one after the other or grouped. Whatever makes sense for your application.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1

                        4/11

                        17 Nov 2017, 18:23

                        topic:navigator.unread, 7
                        • Login

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