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. on Dialog box show() is not working but exec() is
QtWS25 Last Chance

on Dialog box show() is not working but exec() is

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 3 Posters 371 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.
  • R Offline
    R Offline
    rupertrupert
    wrote on 7 Mar 2025, 20:32 last edited by
    #1

    Whenever I exec() a dialog it works but when I do show() instead, i get this weird seethrough box and it eventually crashesalt text

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JonB
      wrote on 7 Mar 2025, 21:17 last edited by JonB 3 Jul 2025, 21:18
      #2

      Do you allow the event loop to be re-entered after the show()? And the dialog instance remains in scope? How does open() behave?

      R 1 Reply Last reply 7 Mar 2025, 21:37
      0
      • J JonB
        7 Mar 2025, 21:17

        Do you allow the event loop to be re-entered after the show()? And the dialog instance remains in scope? How does open() behave?

        R Offline
        R Offline
        rupertrupert
        wrote on 7 Mar 2025, 21:37 last edited by
        #3

        @JonB open() does the exact same thing as show(), and im not sure what you mean about the event loop, but the scope that the dialog is initalized in stays open until someother action happens

        P 1 Reply Last reply 7 Mar 2025, 22:33
        0
        • R rupertrupert
          7 Mar 2025, 21:37

          @JonB open() does the exact same thing as show(), and im not sure what you mean about the event loop, but the scope that the dialog is initalized in stays open until someother action happens

          P Offline
          P Offline
          Pl45m4
          wrote on 7 Mar 2025, 22:33 last edited by Pl45m4 3 Jul 2025, 22:41
          #4

          @rupertrupert

          Show the parts of the code where you open the dialog and what's happening before/after that

          Btw: This looks like an assignment for school class or similar?! So do you have some template which you have to use and extend in order to solve some task(s)?

          And you are not blocking the event loop after calling show() or open() through any while loop or similar, aren't you?!


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          R 2 Replies Last reply 8 Mar 2025, 04:00
          2
          • P Pl45m4
            7 Mar 2025, 22:33

            @rupertrupert

            Show the parts of the code where you open the dialog and what's happening before/after that

            Btw: This looks like an assignment for school class or similar?! So do you have some template which you have to use and extend in order to solve some task(s)?

            And you are not blocking the event loop after calling show() or open() through any while loop or similar, aren't you?!

            R Offline
            R Offline
            rupertrupert
            wrote on 8 Mar 2025, 04:00 last edited by rupertrupert 3 Aug 2025, 04:04
            #5

            @Pl45m4 Yeah I was using while loop with sleep() because I needed a way to update something every second, so im not sure what else to do to implement this

            1 Reply Last reply
            0
            • P Pl45m4
              7 Mar 2025, 22:33

              @rupertrupert

              Show the parts of the code where you open the dialog and what's happening before/after that

              Btw: This looks like an assignment for school class or similar?! So do you have some template which you have to use and extend in order to solve some task(s)?

              And you are not blocking the event loop after calling show() or open() through any while loop or similar, aren't you?!

              R Offline
              R Offline
              rupertrupert
              wrote on 8 Mar 2025, 04:45 last edited by rupertrupert 3 Aug 2025, 04:46
              #6

              @Pl45m4
              Here is what I have right now, allthough update() is never called and I have no idea why
              void Object::start(){
              ui = new Object2 (nullptr);
              ui->open();
              QTimer *timer = new QTimer(this);

              QObject::connect(timer,SIGNAL(timeout()),this,SLOT(update()));
              
              timer->start(1000);
              

              }

              void Object::update(){
              qInfo("Hi");
              }

              P 1 Reply Last reply 8 Mar 2025, 05:16
              0
              • R rupertrupert
                8 Mar 2025, 04:45

                @Pl45m4
                Here is what I have right now, allthough update() is never called and I have no idea why
                void Object::start(){
                ui = new Object2 (nullptr);
                ui->open();
                QTimer *timer = new QTimer(this);

                QObject::connect(timer,SIGNAL(timeout()),this,SLOT(update()));
                
                timer->start(1000);
                

                }

                void Object::update(){
                qInfo("Hi");
                }

                P Offline
                P Offline
                Pl45m4
                wrote on 8 Mar 2025, 05:16 last edited by Pl45m4 3 Aug 2025, 07:44
                #7

                @rupertrupert said in on Dialog box show() is not working but exec() is:

                update() is never called and I have no idea why

                Because your signal calls QWidget::update() and not your Object::update() function.

                • https://doc.qt.io/qt-6/qwidget.html#update

                Rename your function to something like sayHi() and it should work like expected

                Btw: you are leaking memory cause you create a new instance of Object2 every time Object::start() is called. And since your dialog has no valid parent it's not using the auto-cleanup from Qt MetaObject system.

                Add Qt::WA_DeleteOnClose as attribute to your dialog or create in just once in your c'tor, then only open/show it when you need it.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                J 1 Reply Last reply 8 Mar 2025, 09:25
                2
                • P Pl45m4
                  8 Mar 2025, 05:16

                  @rupertrupert said in on Dialog box show() is not working but exec() is:

                  update() is never called and I have no idea why

                  Because your signal calls QWidget::update() and not your Object::update() function.

                  • https://doc.qt.io/qt-6/qwidget.html#update

                  Rename your function to something like sayHi() and it should work like expected

                  Btw: you are leaking memory cause you create a new instance of Object2 every time Object::start() is called. And since your dialog has no valid parent it's not using the auto-cleanup from Qt MetaObject system.

                  Add Qt::WA_DeleteOnClose as attribute to your dialog or create in just once in your c'tor, then only open/show it when you need it.

                  J Offline
                  J Offline
                  JonB
                  wrote on 8 Mar 2025, 09:25 last edited by JonB 3 Aug 2025, 09:26
                  #8

                  @Pl45m4 said in on Dialog box show() is not working but exec() is:

                  Because your signal calls QWidget::update() and not your Object::update() function.

                  Although this would explain behaviour, OOI what makes you say QWidget::update() will be called? The slot is on this, which is a Object with its own update(), and what makes you think Object is a QWidget?

                  Doubtless @Pl45m4 will turn out to be right though I'm not sure why/how he knows. Otherwise what is the scope/lifetime of the Object instance that start() is called on? Or, again, after start() do you allow the event loop to be re-entered?

                  P 1 Reply Last reply 8 Mar 2025, 13:01
                  0
                  • J JonB
                    8 Mar 2025, 09:25

                    @Pl45m4 said in on Dialog box show() is not working but exec() is:

                    Because your signal calls QWidget::update() and not your Object::update() function.

                    Although this would explain behaviour, OOI what makes you say QWidget::update() will be called? The slot is on this, which is a Object with its own update(), and what makes you think Object is a QWidget?

                    Doubtless @Pl45m4 will turn out to be right though I'm not sure why/how he knows. Otherwise what is the scope/lifetime of the Object instance that start() is called on? Or, again, after start() do you allow the event loop to be re-entered?

                    P Offline
                    P Offline
                    Pl45m4
                    wrote on 8 Mar 2025, 13:01 last edited by Pl45m4 3 Aug 2025, 13:52
                    #9

                    @JonB said in on Dialog box show() is not working but exec() is:

                    OOI what makes you say QWidget::update() will be called?

                    The described behavior by OP

                    The slot is on this, which is a Object with its own update()

                    True, but OP uses the String-based connections which are not fully qualified and don't specify a type or class.
                    From my own experience I'm 99.99% sure ;-)

                    what makes you think Object is a QWidget?

                    Must be, otherwise it would have worked ;-)
                    [Edit: Apparently it's actually a QObject but some other weird stuff is going on. Maybe app quit?!]

                    Btw:
                    Using a fully qualified functor will also solve this:

                    // Object::update(), correct
                    QObject::connect(timer, &QTimer::timeout, this, &Object::update);
                    // QWidget::update(), wrong
                    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(update()));
                    

                    &Object::update will explicitely pick void Object::update() by function reference, which prints "Hi" in this case.
                    While the String/Macro based syntax just sends a update() call to this without any checks at runtime, which results in QWidget::update() being called.


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    R 1 Reply Last reply 8 Mar 2025, 13:12
                    0
                    • P Pl45m4
                      8 Mar 2025, 13:01

                      @JonB said in on Dialog box show() is not working but exec() is:

                      OOI what makes you say QWidget::update() will be called?

                      The described behavior by OP

                      The slot is on this, which is a Object with its own update()

                      True, but OP uses the String-based connections which are not fully qualified and don't specify a type or class.
                      From my own experience I'm 99.99% sure ;-)

                      what makes you think Object is a QWidget?

                      Must be, otherwise it would have worked ;-)
                      [Edit: Apparently it's actually a QObject but some other weird stuff is going on. Maybe app quit?!]

                      Btw:
                      Using a fully qualified functor will also solve this:

                      // Object::update(), correct
                      QObject::connect(timer, &QTimer::timeout, this, &Object::update);
                      // QWidget::update(), wrong
                      QObject::connect(timer, SIGNAL(timeout()), this, SLOT(update()));
                      

                      &Object::update will explicitely pick void Object::update() by function reference, which prints "Hi" in this case.
                      While the String/Macro based syntax just sends a update() call to this without any checks at runtime, which results in QWidget::update() being called.

                      R Offline
                      R Offline
                      rupertrupert
                      wrote on 8 Mar 2025, 13:12 last edited by rupertrupert 3 Aug 2025, 13:16
                      #10

                      @Pl45m4 OK i tried both of the solutions you said and they did not work

                      P 1 Reply Last reply 8 Mar 2025, 13:22
                      0
                      • R rupertrupert
                        8 Mar 2025, 13:12

                        @Pl45m4 OK i tried both of the solutions you said and they did not work

                        P Offline
                        P Offline
                        Pl45m4
                        wrote on 8 Mar 2025, 13:22 last edited by Pl45m4 3 Aug 2025, 13:24
                        #11

                        @rupertrupert said in on Dialog box show() is not working but exec() is:

                        OK i tried both of the solutions you said and they did not work

                        If the below code does not work, you are still blocking the signal with some sleep or while - loop.
                        You must not have any sleep or blocking loops in your main/GUI thread in your entire program... otherwise it breaks event driven mechanics as used in Qt.

                        void Object::start()
                        {
                            ui = new Object2(this);
                            ui->open();
                            QTimer *timer = new QTimer(this);
                            QObject::connect(timer, &QTimer::timeout, this, &Object::update);
                            timer->start(1000);
                        }
                        

                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        R 1 Reply Last reply 8 Mar 2025, 13:33
                        1
                        • P Pl45m4
                          8 Mar 2025, 13:22

                          @rupertrupert said in on Dialog box show() is not working but exec() is:

                          OK i tried both of the solutions you said and they did not work

                          If the below code does not work, you are still blocking the signal with some sleep or while - loop.
                          You must not have any sleep or blocking loops in your main/GUI thread in your entire program... otherwise it breaks event driven mechanics as used in Qt.

                          void Object::start()
                          {
                              ui = new Object2(this);
                              ui->open();
                              QTimer *timer = new QTimer(this);
                              QObject::connect(timer, &QTimer::timeout, this, &Object::update);
                              timer->start(1000);
                          }
                          
                          R Offline
                          R Offline
                          rupertrupert
                          wrote on 8 Mar 2025, 13:33 last edited by
                          #12

                          @Pl45m4 said in on Dialog box show() is not working but exec() is:

                          QObject::connect(timer, &QTimer::timeout, this, &Object::update);

                          ok I changed to exactly this and now i dont see my ui or anything and the program just closes when I reach Object::start(), and there is no code after Object::start() is called to be blocking the signal

                          P 1 Reply Last reply 8 Mar 2025, 13:40
                          0
                          • R rupertrupert
                            8 Mar 2025, 13:33

                            @Pl45m4 said in on Dialog box show() is not working but exec() is:

                            QObject::connect(timer, &QTimer::timeout, this, &Object::update);

                            ok I changed to exactly this and now i dont see my ui or anything and the program just closes when I reach Object::start(), and there is no code after Object::start() is called to be blocking the signal

                            P Offline
                            P Offline
                            Pl45m4
                            wrote on 8 Mar 2025, 13:40 last edited by
                            #13

                            @rupertrupert said in on Dialog box show() is not working but exec() is:

                            there is no code after Object::start()

                            Where do you call start()? In MainWindow? Or in your main.cpp?

                            How is your Object created and what kind of class is this? As @JonB was also wondering...


                            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                            ~E. W. Dijkstra

                            R 1 Reply Last reply 8 Mar 2025, 13:45
                            1
                            • P Pl45m4
                              8 Mar 2025, 13:40

                              @rupertrupert said in on Dialog box show() is not working but exec() is:

                              there is no code after Object::start()

                              Where do you call start()? In MainWindow? Or in your main.cpp?

                              How is your Object created and what kind of class is this? As @JonB was also wondering...

                              R Offline
                              R Offline
                              rupertrupert
                              wrote on 8 Mar 2025, 13:45 last edited by rupertrupert 3 Aug 2025, 13:49
                              #14

                              @Pl45m4 after my main window is closed, in the signal that closes it, and it is a QObject

                              P 1 Reply Last reply 8 Mar 2025, 13:49
                              0
                              • R rupertrupert
                                8 Mar 2025, 13:45

                                @Pl45m4 after my main window is closed, in the signal that closes it, and it is a QObject

                                P Offline
                                P Offline
                                Pl45m4
                                wrote on 8 Mar 2025, 13:49 last edited by
                                #15

                                @rupertrupert said in on Dialog box show() is not working but exec() is:

                                after my main window is closed

                                Usually your program would end at this point...
                                Post the related code then... this isn't a Q&A game here.
                                We can't see what your are doing exactly


                                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                ~E. W. Dijkstra

                                1 Reply Last reply
                                1

                                4/15

                                7 Mar 2025, 22:33

                                topic:navigator.unread, 11
                                • Login

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