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 display the result of an operation in incremental fashion strating from 0?
Forum Updated to NodeBB v4.3 + New Features

How to display the result of an operation in incremental fashion strating from 0?

Scheduled Pinned Locked Moved Unsolved General and Desktop
34 Posts 8 Posters 3.9k 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.
  • jsulmJ jsulm

    @Santhoshpl You did not write what the problem is, but I think I know what happens: you see the result only when the loop finishes, right? Not while it runs. The problem is: the for-loop is blocking the Qt event loop as long as it is running, so the UI will not be updated until the loop finishes.

    Or do you mean that you only see the last result? If this is the case than please tell us what ui->product is and what ui->product->display does.

    S Offline
    S Offline
    Santhoshpl
    wrote on last edited by Santhoshpl
    #4

    @jsulm Yes, you are right. I am seeing only the end result after the loop exits.
    I want to update the UI as it runs. The ui->product->display(product) displays the result in the UI.

    JonBJ 1 Reply Last reply
    0
    • S Santhoshpl

      @jsulm Yes, you are right. I am seeing only the end result after the loop exits.
      I want to update the UI as it runs. The ui->product->display(product) displays the result in the UI.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #5

      @Santhoshpl So try @LeLev's suggested code.

      1 Reply Last reply
      0
      • ODБOïO ODБOï

        hi
        @Santhoshpl said in How to display the result of an operation in incremental fashion strating from 0?:

        double num1 = ui->num1->value();
        double num2 = ui->num2->value();
        double product= num1*num2;
        int temp=0;
        for(int i = 0; i<product; i++){
        temp++;
        ui->product->display(temp);

        you can use QTimer for that

         double num1 = 10;
            double num2 = 2;
            double product = num1*num2;
        
            int currentDisplay=0; //<
        
            QTimer *timer = new QTimer();
            QObject::connect(timer, &QTimer::timeout,[&](){
                if(currentDisplay>=product){
                    timer->stop();
                }
                qDebug()<<currentDisplay; // ui->product->display(temp);
                if(currentDisplay<product)currentDisplay++;
            });
            timer->start(50);
        
        S Offline
        S Offline
        Santhoshpl
        wrote on last edited by
        #6

        @LeLev Thank you for the solution, but its crashing.
        The error message displayed is:
        The program has unexpectedly finished.
        The process was ended forcefully.
        productApplication.exe is crashed

        ODБOïO 1 Reply Last reply
        0
        • S Santhoshpl

          @LeLev Thank you for the solution, but its crashing.
          The error message displayed is:
          The program has unexpectedly finished.
          The process was ended forcefully.
          productApplication.exe is crashed

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by ODБOï
          #7

          @Santhoshpl said in How to display the result of an operation in incremental fashion strating from 0?:

          crash

          can you please show your code ?

          S 1 Reply Last reply
          0
          • G Offline
            G Offline
            Gerd
            wrote on last edited by
            #8

            or..
            just add a
            QApplication::processEvents();

            right after

            ui->product->display(temp);

            jsulmJ 1 Reply Last reply
            -1
            • ODБOïO ODБOï

              @Santhoshpl said in How to display the result of an operation in incremental fashion strating from 0?:

              crash

              can you please show your code ?

              S Offline
              S Offline
              Santhoshpl
              wrote on last edited by Santhoshpl
              #9

              @LeLev please find the below code:

              void MainWindow::on_controlButton_clicked()
              {
              double num1 = ui->num1->value();
              double num2 = ui->num2->value();
              double product = num1 * num2;
              double currentDisplay=0;
              QTimer *timer = new QTimer();
              QObject::connect(timer, &QTimer::timeout,&{
              if(currentDisplay>=product){
              timer->stop();
              }
              ui->product->display(currentDisplay);
              QApplication::processEvents();
              if(currentDisplay<meetingCost)currentDisplay++;
              });
              timer->start(500);
              }
              }

              JonBJ 1 Reply Last reply
              0
              • S Santhoshpl

                @LeLev please find the below code:

                void MainWindow::on_controlButton_clicked()
                {
                double num1 = ui->num1->value();
                double num2 = ui->num2->value();
                double product = num1 * num2;
                double currentDisplay=0;
                QTimer *timer = new QTimer();
                QObject::connect(timer, &QTimer::timeout,&{
                if(currentDisplay>=product){
                timer->stop();
                }
                ui->product->display(currentDisplay);
                QApplication::processEvents();
                if(currentDisplay<meetingCost)currentDisplay++;
                });
                timer->start(500);
                }
                }

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #10

                @Santhoshpl
                It helps others if you put literal code in this forum's code markers.

                Meanwhile, I don't know, @Lelev writes

                 QObject::connect(timer, &QTimer::timeout,[&](){
                

                while you write

                QObject::connect(timer, &QTimer::timeout,&{
                

                so do you know what you're doing there with your lambda? Is there any reason you change his code for this?

                Otherwise run under debugger and look at the context/stack trace when the crash occurs.

                1 Reply Last reply
                3
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #11

                  @JonB said in How to display the result of an operation in incremental fashion strating from 0?:

                  [&]

                  Please take a c++ book about lambdas - you're passing by reference and it will therefore go out of scope...

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  6
                  • G Gerd

                    or..
                    just add a
                    QApplication::processEvents();

                    right after

                    ui->product->display(temp);

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

                    @Gerd said in How to display the result of an operation in incremental fashion strating from 0?:

                    QApplication::processEvents();

                    This is an ugly work-around and should be avoided. It is better to program in asynchronous way instead of calling processEvents().

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

                    1 Reply Last reply
                    5
                    • G Offline
                      G Offline
                      Gerd
                      wrote on last edited by
                      #13

                      lol
                      then you should tell the qt people to remove such examples from the examples in qt sources...

                      J.HilkJ 1 Reply Last reply
                      0
                      • G Gerd

                        lol
                        then you should tell the qt people to remove such examples from the examples in qt sources...

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

                        @Gerd said in How to display the result of an operation in incremental fashion strating from 0?:

                        then you should tell the qt people to remove such examples from the examples in qt sources...

                        you're right, it shouldn't be there, can you name/link such an example?


                        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
                        0
                        • G Offline
                          G Offline
                          Gerd
                          wrote on last edited by
                          #15

                          Qt\5.12.6.src\qtbase\examples\widgets\dialogs\findfiles
                          Qt\5.12.6.src\qtbase\examples\widgets\itemviews\pixelator
                          Qt\5.12.6.src\qtbase\examples\widgets\painting\fontsampler

                          JonBJ 1 Reply Last reply
                          1
                          • G Gerd

                            Qt\5.12.6.src\qtbase\examples\widgets\dialogs\findfiles
                            Qt\5.12.6.src\qtbase\examples\widgets\itemviews\pixelator
                            Qt\5.12.6.src\qtbase\examples\widgets\painting\fontsampler

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #16

                            @Gerd Can you supply these as clickable links to the on-line docs?

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              Gerd
                              wrote on last edited by
                              #17

                              heres one of them:
                              findfiles

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

                                for these it isnt in the doc pages but in the sourcecode..
                                pixelator
                                fontsampler

                                J.HilkJ 1 Reply Last reply
                                1
                                • G Gerd

                                  for these it isnt in the doc pages but in the sourcecode..
                                  pixelator
                                  fontsampler

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

                                  @Gerd
                                  Well these examples haven't been touched in years, are very basic but never the less badly designed.

                                  One can redesign those examples, without the need for a processEvent calls, which will make them bigger (source code size).
                                  But IMHO that's something that should be 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
                                  • ODБOïO ODБOï

                                    hi
                                    @Santhoshpl said in How to display the result of an operation in incremental fashion strating from 0?:

                                    double num1 = ui->num1->value();
                                    double num2 = ui->num2->value();
                                    double product= num1*num2;
                                    int temp=0;
                                    for(int i = 0; i<product; i++){
                                    temp++;
                                    ui->product->display(temp);

                                    you can use QTimer for that

                                     double num1 = 10;
                                        double num2 = 2;
                                        double product = num1*num2;
                                    
                                        int currentDisplay=0; //<
                                    
                                        QTimer *timer = new QTimer();
                                        QObject::connect(timer, &QTimer::timeout,[&](){
                                            if(currentDisplay>=product){
                                                timer->stop();
                                            }
                                            qDebug()<<currentDisplay; // ui->product->display(temp);
                                            if(currentDisplay<product)currentDisplay++;
                                        });
                                        timer->start(50);
                                    
                                    C Offline
                                    C Offline
                                    Colins2
                                    wrote on last edited by
                                    #20

                                    @LeLev
                                    I just read your response and it seemed what I need as well.
                                    However, I just get a crash with this code:

                                    I have been having the same problems as the OP (and everyone else, I guess)
                                    This code snippet is just for testing messages written to a QPlainTextEdit widget, nothing else.

                                    void MainWindow::writeLeft()
                                    {
                                        int32_t cnt = 100;
                                        QString msg, cno;
                                        QTimer *ltimer = new QTimer();
                                    
                                        do{
                                            QTextStream(&cno) << cnt;
                                            msg = ("Hello left no : "+ cno);
                                            //message has been constructed, stop the process and display it
                                            QObject::connect(ltimer, &QTimer::timeout,[&](){
                                                if(cnt > 0){
                                                    ltimer->stop();
                                                }
                                            ui->memo1->appendPlainText(msg);
                                            });
                                            cno.clear();
                                            QThread::sleep(1);
                                            ltimer->start(10);
                                            cnt--;
                                        }while(cnt > 0);
                                    }
                                    

                                    If I remove the QTimer statements then I just get the whole lot dumped to the QPlainTextEdit in one go.

                                    JonBJ ODБOïO 2 Replies Last reply
                                    0
                                    • C Colins2

                                      @LeLev
                                      I just read your response and it seemed what I need as well.
                                      However, I just get a crash with this code:

                                      I have been having the same problems as the OP (and everyone else, I guess)
                                      This code snippet is just for testing messages written to a QPlainTextEdit widget, nothing else.

                                      void MainWindow::writeLeft()
                                      {
                                          int32_t cnt = 100;
                                          QString msg, cno;
                                          QTimer *ltimer = new QTimer();
                                      
                                          do{
                                              QTextStream(&cno) << cnt;
                                              msg = ("Hello left no : "+ cno);
                                              //message has been constructed, stop the process and display it
                                              QObject::connect(ltimer, &QTimer::timeout,[&](){
                                                  if(cnt > 0){
                                                      ltimer->stop();
                                                  }
                                              ui->memo1->appendPlainText(msg);
                                              });
                                              cno.clear();
                                              QThread::sleep(1);
                                              ltimer->start(10);
                                              cnt--;
                                          }while(cnt > 0);
                                      }
                                      

                                      If I remove the QTimer statements then I just get the whole lot dumped to the QPlainTextEdit in one go.

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #21

                                      @Colins2
                                      You are re-connecting the timer every time round the loop?

                                      C 1 Reply Last reply
                                      2
                                      • C Colins2

                                        @LeLev
                                        I just read your response and it seemed what I need as well.
                                        However, I just get a crash with this code:

                                        I have been having the same problems as the OP (and everyone else, I guess)
                                        This code snippet is just for testing messages written to a QPlainTextEdit widget, nothing else.

                                        void MainWindow::writeLeft()
                                        {
                                            int32_t cnt = 100;
                                            QString msg, cno;
                                            QTimer *ltimer = new QTimer();
                                        
                                            do{
                                                QTextStream(&cno) << cnt;
                                                msg = ("Hello left no : "+ cno);
                                                //message has been constructed, stop the process and display it
                                                QObject::connect(ltimer, &QTimer::timeout,[&](){
                                                    if(cnt > 0){
                                                        ltimer->stop();
                                                    }
                                                ui->memo1->appendPlainText(msg);
                                                });
                                                cno.clear();
                                                QThread::sleep(1);
                                                ltimer->start(10);
                                                cnt--;
                                            }while(cnt > 0);
                                        }
                                        

                                        If I remove the QTimer statements then I just get the whole lot dumped to the QPlainTextEdit in one go.

                                        ODБOïO Offline
                                        ODБOïO Offline
                                        ODБOï
                                        wrote on last edited by ODБOï
                                        #22

                                        @Colins2 im not 100% sure what you need to do but see

                                        
                                            int32_t cnt = 100;
                                            QString msg, cno;
                                            QTimer *ltimer = new QTimer();
                                        
                                            //do{
                                            // QTextStream(&cno) << cnt;
                                        
                                            //msg = ("Hello left no : "+ cno);
                                        
                                            QObject::connect(ltimer, &QTimer::timeout,[&](){
                                                cnt--;
                                                if(cnt == 0){ // <
                                                    ltimer->stop();
                                                }
                                                qDebug()<< ("Hello left no : " + QString::number(cnt));
                                        
                                            });
                                        
                                            cno.clear();
                                            //  QThread::sleep(1);
                                            ltimer->start(10);
                                        
                                            //   }while(cnt > 0);
                                        
                                        JonBJ C 2 Replies Last reply
                                        1
                                        • ODБOïO ODБOï

                                          @Colins2 im not 100% sure what you need to do but see

                                          
                                              int32_t cnt = 100;
                                              QString msg, cno;
                                              QTimer *ltimer = new QTimer();
                                          
                                              //do{
                                              // QTextStream(&cno) << cnt;
                                          
                                              //msg = ("Hello left no : "+ cno);
                                          
                                              QObject::connect(ltimer, &QTimer::timeout,[&](){
                                                  cnt--;
                                                  if(cnt == 0){ // <
                                                      ltimer->stop();
                                                  }
                                                  qDebug()<< ("Hello left no : " + QString::number(cnt));
                                          
                                              });
                                          
                                              cno.clear();
                                              //  QThread::sleep(1);
                                              ltimer->start(10);
                                          
                                              //   }while(cnt > 0);
                                          
                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by JonB
                                          #23

                                          @Colins2
                                          Glancing only at @LeLev's latest post, this makes much more sense. Give it a go?!

                                          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