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. Does a loop continue running even if Signals and slot is not complete?
QtWS25 Last Chance

Does a loop continue running even if Signals and slot is not complete?

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 5 Posters 3.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.
  • G Offline
    G Offline
    GCDX
    wrote on last edited by
    #1

    0_1530603344305_34ea8264-5de8-423b-98ae-e94895f8340c-image.png

    Hi everyone, this is the code i've written.
    Basically, my code has errors that cause it to terminate which i can't debug.

    But, the main question i'm trying to ask, is whether while loops will continue its next iteration even before the signal and slot is done.

    So, in the code, i'm looping through a map to determine the key and value inside. For each set of key and value pair, i'm going to use a connect to allow the webpage to load finished and then bring it to parse2(bool) to parse information. So far, it has not been giving me the right outcome and sometimes it says "QWaitCondition: Destroyed while threads are still waiting".
    But, i was wondering is it because the next iteration of the while loop causes this error to come out because it takes time for a webpage to load and to parse the information out?

    I have no idea how to test this out or debug.

    1 Reply Last reply
    0
    • kkoehneK Offline
      kkoehneK Offline
      kkoehne
      Moderators
      wrote on last edited by
      #2

      A QObject::connect() does not call in to any other code of yours; in particular, it won't execute slots. The same is true for all the other calls yo made, so you should be safe there.

      Your problem must be elsewhere.

      Director R&D, The Qt Company

      G 1 Reply Last reply
      4
      • G Offline
        G Offline
        GCDX
        wrote on last edited by
        #3

        @kkoehne just to be sure, the connect() function will cause my parse2(bool) to be complete before executing the next loop?

        jsulmJ 1 Reply Last reply
        0
        • G GCDX

          @kkoehne just to be sure, the connect() function will cause my parse2(bool) to be complete before executing the next loop?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @GCDX Again: connect does NOT call the slot, it just connects the signal to the slot. The slot (parse2() in your case) will be called as soon as the signal is emitted. Please read http://doc.qt.io/qt-5.9/signalsandslots.html

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          4
          • G Offline
            G Offline
            GCDX
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • kkoehneK kkoehne

              A QObject::connect() does not call in to any other code of yours; in particular, it won't execute slots. The same is true for all the other calls yo made, so you should be safe there.

              Your problem must be elsewhere.

              G Offline
              G Offline
              GCDX
              wrote on last edited by
              #6

              @kkoehne @jsulm
              Okay i get it! but basically, it links a signal and a slot, when the signal is emitted, the corresponding slot will be called.

              My question is in pseudo code:

              connect(object,SIGNAL(function1()),SLOT(function2());
              int x=0;
              while(true when has next value){
              x++;
              print(x);
              function1();
              }
              function2(){print("executed last");}

              The problem i'm facing now is, the for loop will execute finish before function2.

              output:
              1
              2
              executed last

              Why is this so? shouldnt it be, the while will execute function1 first and then 2 and then run the next iteration?

              6thC6 jsulmJ 2 Replies Last reply
              0
              • G GCDX

                @kkoehne @jsulm
                Okay i get it! but basically, it links a signal and a slot, when the signal is emitted, the corresponding slot will be called.

                My question is in pseudo code:

                connect(object,SIGNAL(function1()),SLOT(function2());
                int x=0;
                while(true when has next value){
                x++;
                print(x);
                function1();
                }
                function2(){print("executed last");}

                The problem i'm facing now is, the for loop will execute finish before function2.

                output:
                1
                2
                executed last

                Why is this so? shouldnt it be, the while will execute function1 first and then 2 and then run the next iteration?

                6thC6 Offline
                6thC6 Offline
                6thC
                wrote on last edited by
                #7

                @GCDX
                Unrelated, but you'll (usually) do yourself a favor using the new syntax https://wiki.qt.io/New_Signal_Slot_Syntax: :you get type checks at compile time.
                You can also hit F2 on your sig/slot methods and navigate direct instead of hunt.

                I don't have experience using / abusing / this style, I'm pretty sure it's wrong for the old sytax, pretty sure you need to specify a receiver object instance.

                connect(object,SIGNAL(function1()),SLOT(function2());// nearly certain this is invalid as you have no receiver object set but use SLOT
                

                can become:

                connect(object, &ObjectT::function1, whateverObjectThisWasMeantToBe, &OtherOrSameObjectT::function2); 
                //or
                connect(object, &ObjectT::function1, function2); 
                
                //or even a lambda expression!
                

                You should also see this: http://doc.qt.io/qt-5/threads-qobject.html#signals-and-slots-across-threads

                jsulmJ J.HilkJ 2 Replies Last reply
                1
                • G GCDX

                  @kkoehne @jsulm
                  Okay i get it! but basically, it links a signal and a slot, when the signal is emitted, the corresponding slot will be called.

                  My question is in pseudo code:

                  connect(object,SIGNAL(function1()),SLOT(function2());
                  int x=0;
                  while(true when has next value){
                  x++;
                  print(x);
                  function1();
                  }
                  function2(){print("executed last");}

                  The problem i'm facing now is, the for loop will execute finish before function2.

                  output:
                  1
                  2
                  executed last

                  Why is this so? shouldnt it be, the while will execute function1 first and then 2 and then run the next iteration?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @GCDX Did you really try this pseudo code, I mean as real code?
                  If you connect like this:

                  connect(object,SIGNAL(function1()),SLOT(function2());
                  

                  then it will be direct connection. That means: as soon as the signal is emitted all connected slots will be executed. This is more or less same as calling all connected slots directly.
                  So here

                  connect(object,SIGNAL(function1()),SLOT(function2());
                  int x=0;
                  while(true when has next value){
                      x++;
                      print(x);
                      function1();
                  }
                  function2(){print("executed last");}
                  

                  I would expect
                  1
                  executed last
                  2
                  executed last
                  as output.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  3
                  • 6thC6 6thC

                    @GCDX
                    Unrelated, but you'll (usually) do yourself a favor using the new syntax https://wiki.qt.io/New_Signal_Slot_Syntax: :you get type checks at compile time.
                    You can also hit F2 on your sig/slot methods and navigate direct instead of hunt.

                    I don't have experience using / abusing / this style, I'm pretty sure it's wrong for the old sytax, pretty sure you need to specify a receiver object instance.

                    connect(object,SIGNAL(function1()),SLOT(function2());// nearly certain this is invalid as you have no receiver object set but use SLOT
                    

                    can become:

                    connect(object, &ObjectT::function1, whateverObjectThisWasMeantToBe, &OtherOrSameObjectT::function2); 
                    //or
                    connect(object, &ObjectT::function1, function2); 
                    
                    //or even a lambda expression!
                    

                    You should also see this: http://doc.qt.io/qt-5/threads-qobject.html#signals-and-slots-across-threads

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by jsulm
                    #9

                    @GCDX I just tried this code:

                    connect(this, SIGNAL(somethingHappend()), this, SLOT(doSomething()));
                    for (int i = 0; i < 2; ++i) {
                        qDebug() << i;
                        emit somethingHappend();
                    }
                    
                    void MainWindow::doSomething()
                    {
                        qDebug() << "do something";
                    }
                    

                    Output was as expected:
                    0
                    do something
                    1
                    do something

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    6thC6 G 2 Replies Last reply
                    2
                    • jsulmJ jsulm

                      @GCDX I just tried this code:

                      connect(this, SIGNAL(somethingHappend()), this, SLOT(doSomething()));
                      for (int i = 0; i < 2; ++i) {
                          qDebug() << i;
                          emit somethingHappend();
                      }
                      
                      void MainWindow::doSomething()
                      {
                          qDebug() << "do something";
                      }
                      

                      Output was as expected:
                      0
                      do something
                      1
                      do something

                      6thC6 Offline
                      6thC6 Offline
                      6thC
                      wrote on last edited by
                      #10

                      @jsulm yes :-) specifying the receiving object ( this ptr /any obj/instance really) is how I'd expect old sytnax to work too. I didn't see it in the other code, looked like a hybrid old and new syntax. Didn't feel right.

                      1 Reply Last reply
                      1
                      • 6thC6 6thC

                        @GCDX
                        Unrelated, but you'll (usually) do yourself a favor using the new syntax https://wiki.qt.io/New_Signal_Slot_Syntax: :you get type checks at compile time.
                        You can also hit F2 on your sig/slot methods and navigate direct instead of hunt.

                        I don't have experience using / abusing / this style, I'm pretty sure it's wrong for the old sytax, pretty sure you need to specify a receiver object instance.

                        connect(object,SIGNAL(function1()),SLOT(function2());// nearly certain this is invalid as you have no receiver object set but use SLOT
                        

                        can become:

                        connect(object, &ObjectT::function1, whateverObjectThisWasMeantToBe, &OtherOrSameObjectT::function2); 
                        //or
                        connect(object, &ObjectT::function1, function2); 
                        
                        //or even a lambda expression!
                        

                        You should also see this: http://doc.qt.io/qt-5/threads-qobject.html#signals-and-slots-across-threads

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #11

                        @6thC
                        take a look at this connect overload:
                        http://doc.qt.io/qt-5/qobject.html#connect-2

                        you can omit the receiver object, connect falls than back to this as the receiver. Not consistent and OCD-triggering, but its a valid connect statement.


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        1
                        • jsulmJ jsulm

                          @GCDX I just tried this code:

                          connect(this, SIGNAL(somethingHappend()), this, SLOT(doSomething()));
                          for (int i = 0; i < 2; ++i) {
                              qDebug() << i;
                              emit somethingHappend();
                          }
                          
                          void MainWindow::doSomething()
                          {
                              qDebug() << "do something";
                          }
                          

                          Output was as expected:
                          0
                          do something
                          1
                          do something

                          G Offline
                          G Offline
                          GCDX
                          wrote on last edited by
                          #12

                          @jsulm hmmm yup thanks i found my error but i don't know how to fix it.
                          void MainWindow::on_pushButton_clicked()
                          {
                          QString modules=stringpath;
                          QUrl url2(modules);
                          innerPage->setUrl(url2);//innerpage is a QWebview
                          connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool)));
                          }

                          void MainWindow::parse(bool)
                          {
                          QWebFrame *frameInner = innerPage->page()->mainFrame();
                          QWebElement doc = frameInner->documentElement();
                          QString json=doc.toPlainText();
                          //parsing code
                          print("parsing executed");
                          }

                          i realised i put on_pushButton_clicked in a loop as such:
                          int i=0;
                          While(i!=2){
                          i++;
                          print(i)
                          on_pushbutton_clicked();
                          }
                          outcome:
                          1
                          2
                          parsing executed.

                          i want parsing so be executed in every loop however i cant seem to be able to solve it?

                          J.HilkJ jsulmJ 2 Replies Last reply
                          0
                          • G GCDX

                            @jsulm hmmm yup thanks i found my error but i don't know how to fix it.
                            void MainWindow::on_pushButton_clicked()
                            {
                            QString modules=stringpath;
                            QUrl url2(modules);
                            innerPage->setUrl(url2);//innerpage is a QWebview
                            connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool)));
                            }

                            void MainWindow::parse(bool)
                            {
                            QWebFrame *frameInner = innerPage->page()->mainFrame();
                            QWebElement doc = frameInner->documentElement();
                            QString json=doc.toPlainText();
                            //parsing code
                            print("parsing executed");
                            }

                            i realised i put on_pushButton_clicked in a loop as such:
                            int i=0;
                            While(i!=2){
                            i++;
                            print(i)
                            on_pushbutton_clicked();
                            }
                            outcome:
                            1
                            2
                            parsing executed.

                            i want parsing so be executed in every loop however i cant seem to be able to solve it?

                            J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #13

                            @GCDX said in Does a loop continue running even if Signals and slot is not complete?:
                            ok first of all remove that line from the on_pushButton_clicked and place it in your constructor of MainWindow

                            connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool)));

                            otherwise you create additional connections each time you call on_pushButton_clicked.
                            -> 2nd time on_pushButton_clicked is called -> parse is called twice
                            -> 3ed timeon_pushButton_clicked is called -> parse is called 3 times
                            etc.

                            2nd, make a container, QList or QVector for example, that contains all of your targeted urls, create a member variable cnt to help you navigate through the container.

                            in on pushbutton clicked you set cnt to 0 and start loading the first url.

                            on load finished, you increase cnt by 1, check if its still smaler that your url list. If it is, load next one, if not, your're done.


                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            1 Reply Last reply
                            2
                            • G GCDX

                              @jsulm hmmm yup thanks i found my error but i don't know how to fix it.
                              void MainWindow::on_pushButton_clicked()
                              {
                              QString modules=stringpath;
                              QUrl url2(modules);
                              innerPage->setUrl(url2);//innerpage is a QWebview
                              connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool)));
                              }

                              void MainWindow::parse(bool)
                              {
                              QWebFrame *frameInner = innerPage->page()->mainFrame();
                              QWebElement doc = frameInner->documentElement();
                              QString json=doc.toPlainText();
                              //parsing code
                              print("parsing executed");
                              }

                              i realised i put on_pushButton_clicked in a loop as such:
                              int i=0;
                              While(i!=2){
                              i++;
                              print(i)
                              on_pushbutton_clicked();
                              }
                              outcome:
                              1
                              2
                              parsing executed.

                              i want parsing so be executed in every loop however i cant seem to be able to solve it?

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by jsulm
                              #14

                              @GCDX This is easy: MainWindow::parse(bool) will be called as soon as innerPage emits the signal loadFinished, it does not care about your loop. In fact your while() loop is blocking the event loop, so MainWindow::parse(bool) will NOT be called as long as your loop is running.

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                GCDX
                                wrote on last edited by
                                #15

                                @jsulm how do a rewrite it so that it will work???

                                @J-Hilk my method is what you describe which doesn't work! something is preventing the parse from running until the while loop is completed

                                jsulmJ J.HilkJ 2 Replies Last reply
                                0
                                • G GCDX

                                  @jsulm how do a rewrite it so that it will work???

                                  @J-Hilk my method is what you describe which doesn't work! something is preventing the parse from running until the while loop is completed

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @GCDX said in Does a loop continue running even if Signals and slot is not complete?:

                                  how do a rewrite it so that it will work???

                                  I don't understand what you actually want to achieve.
                                  I don't understand why you call on_pushbutton_clicked() inside the loop.
                                  If you want to call something immediately inside the loop then simply call the method instead of emitting a signal.

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • G GCDX

                                    @jsulm how do a rewrite it so that it will work???

                                    @J-Hilk my method is what you describe which doesn't work! something is preventing the parse from running until the while loop is completed

                                    J.HilkJ Offline
                                    J.HilkJ Offline
                                    J.Hilk
                                    Moderators
                                    wrote on last edited by
                                    #17

                                    @GCDX said in Does a loop continue running even if Signals and slot is not complete?:

                                    something is preventing the parse from running

                                    Yes, your while loop! What I describted eleminates the loop and manages the next url just, when the first parse is done


                                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                    Q: What's that?
                                    A: It's blue light.
                                    Q: What does it do?
                                    A: It turns blue.

                                    1 Reply Last reply
                                    2
                                    • G Offline
                                      G Offline
                                      GCDX
                                      wrote on last edited by
                                      #18

                                      @J-Hilk but don't you still need a for loop to execute what you said?

                                      J.HilkJ 1 Reply Last reply
                                      0
                                      • G GCDX

                                        @J-Hilk but don't you still need a for loop to execute what you said?

                                        J.HilkJ Offline
                                        J.HilkJ Offline
                                        J.Hilk
                                        Moderators
                                        wrote on last edited by
                                        #19

                                        @GCDX
                                        untested of course.

                                        MainWindow::MainWindow(...){
                                            ....
                                            connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool)));
                                            urlList = QList<QUrl> ({QUrl("Url1"), QUrl("Url"), ...});
                                        }
                                        
                                        void MainWindow::continue(){
                                             if(cnt < urlList.size()){
                                                 innerPage->setUrl(urlList.at(cnt));      
                                                 print(cnt); 
                                             } 
                                        }
                                        
                                        void MainWindow::on_pushButton_clicked()
                                        {
                                           cnt = 0;
                                           continue();
                                        }
                                        
                                        void MainWindow::parse(bool)
                                        {
                                        QWebFrame *frameInner = innerPage->page()->mainFrame();
                                        QWebElement doc = frameInner->documentElement();
                                        QString json=doc.toPlainText();
                                        //parsing code
                                        print("parsing executed");
                                           continue();
                                        }
                                        }
                                        

                                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                        Q: What's that?
                                        A: It's blue light.
                                        Q: What does it do?
                                        A: It turns blue.

                                        6thC6 1 Reply Last reply
                                        3
                                        • G Offline
                                          G Offline
                                          GCDX
                                          wrote on last edited by
                                          #20

                                          @J-Hilk wow it works, thanks a lot, its a very smart solution that i would never have thought of!!! Thanks so much :)

                                          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