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
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 3 Posters 769 Views 2 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.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #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
    0
    • JonBJ JonB

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

      Pl45m4P 1 Reply Last reply
      0
      • R rupertrupert

        @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

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #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
        2
        • Pl45m4P Pl45m4

          @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 last edited by rupertrupert
          #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
          • Pl45m4P Pl45m4

            @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 last edited by rupertrupert
            #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");
            }

            Pl45m4P 1 Reply Last reply
            0
            • R rupertrupert

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

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #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

              JonBJ 1 Reply Last reply
              2
              • Pl45m4P Pl45m4

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #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?

                Pl45m4P 1 Reply Last reply
                0
                • JonBJ JonB

                  @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?

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by Pl45m4
                  #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
                  0
                  • Pl45m4P Pl45m4

                    @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 last edited by rupertrupert
                    #10

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

                    Pl45m4P 1 Reply Last reply
                    0
                    • R rupertrupert

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

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on last edited by Pl45m4
                      #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
                      1
                      • Pl45m4P Pl45m4

                        @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 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

                        Pl45m4P 1 Reply Last reply
                        0
                        • R rupertrupert

                          @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

                          Pl45m4P Offline
                          Pl45m4P Offline
                          Pl45m4
                          wrote on 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
                          1
                          • Pl45m4P Pl45m4

                            @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 last edited by rupertrupert
                            #14

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

                            Pl45m4P 1 Reply Last reply
                            0
                            • R rupertrupert

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

                              Pl45m4P Offline
                              Pl45m4P Offline
                              Pl45m4
                              wrote on 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

                              • Login

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