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.5k 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.
  • 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
                • G Offline
                  G Offline
                  Gerd
                  wrote on last edited by
                  #24

                  @J-Hilk
                  I totally disagree with that. What you are doing here is to replace a simple loop routine with an overcomplicated timer driven solution.
                  In such simple programs there is absolutly no needance for that.
                  Keep it simple and its runs (fast)...

                  1 Reply Last reply
                  0
                  • 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);
                    
                    C Offline
                    C Offline
                    Colins2
                    wrote on last edited by
                    #25

                    @LeLev
                    I don't think your solution is what I want.

                    What I am trying to achieve is quite simple - or should be.

                    I have a PlainTextEdit widget that I am using as a log output.
                    At various times through different routines, I want to output a message.
                    What happens is that I get no output until the end of the routine, which isn't much use.

                    I just cobbled the above routine to try to get just the message output working. It has no real relevance to my project other than the method.
                    My loop just created a message with the counter attached and I would expect it to print to the PlainTextEdit widget when called.
                    It doesn't do that. Stepping through the code, the statement executes and then the loop continues but nothing appears in the edit field. At the end of the loop it all gets dumped in one go.

                    Re. the QTimer, It's new to me. I've never had to use these types of function to get output on the screen.
                    As Gerd says in his post, it should be such a simple thing to do but it appears not to be.

                    jsulmJ 1 Reply Last reply
                    0
                    • JonBJ JonB

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

                      C Offline
                      C Offline
                      Colins2
                      wrote on last edited by
                      #26

                      @JonB
                      The idea was to trigger the timer stop so that I could print the current message and then resume.
                      I see what you mean though, it shouldn't be necessary to re-connect every time, just start and stop.

                      JonBJ 1 Reply Last reply
                      0
                      • C Colins2

                        @LeLev
                        I don't think your solution is what I want.

                        What I am trying to achieve is quite simple - or should be.

                        I have a PlainTextEdit widget that I am using as a log output.
                        At various times through different routines, I want to output a message.
                        What happens is that I get no output until the end of the routine, which isn't much use.

                        I just cobbled the above routine to try to get just the message output working. It has no real relevance to my project other than the method.
                        My loop just created a message with the counter attached and I would expect it to print to the PlainTextEdit widget when called.
                        It doesn't do that. Stepping through the code, the statement executes and then the loop continues but nothing appears in the edit field. At the end of the loop it all gets dumped in one go.

                        Re. the QTimer, It's new to me. I've never had to use these types of function to get output on the screen.
                        As Gerd says in his post, it should be such a simple thing to do but it appears not to be.

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

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

                        At various times through different routines, I want to output a message.

                        If I understand you correctly this will not happen in a loop in your main thread - is that correct?
                        Is your current implementation with the loop just proof of concept (or an example)?
                        As I explained already your loop is blocking the event loop that's why it behaves like it does.
                        So, the question is: how are these messages generated and exposed?

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

                        C 1 Reply Last reply
                        1
                        • C Colins2

                          @JonB
                          The idea was to trigger the timer stop so that I could print the current message and then resume.
                          I see what you mean though, it shouldn't be necessary to re-connect every time, just start and stop.

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

                          @Colins2
                          It's not just that it shouldn't be necessary, your loop will create a brand new, duplicate connection each time round the loop. You will end up with hundreds of slots on the timer's signal.

                          This code really shouldn't be difficult! Though I'm not offering to write, and I'm Python/PyQt anyway. You have to have something which allows the Qt event loop to run if you want to see the output come up as you go along. If your new data arrives from somewhere externally, you might raise a signal and have a slot to update your display. That will be happening within the Qt main event loop, so all will get updated as you go along.

                          I see @jsulm is responding more to what you want, I'll leave it to him :)

                          1 Reply Last reply
                          0
                          • jsulmJ jsulm

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

                            At various times through different routines, I want to output a message.

                            If I understand you correctly this will not happen in a loop in your main thread - is that correct?
                            Is your current implementation with the loop just proof of concept (or an example)?
                            As I explained already your loop is blocking the event loop that's why it behaves like it does.
                            So, the question is: how are these messages generated and exposed?

                            C Offline
                            C Offline
                            Colins2
                            wrote on last edited by
                            #29

                            @jsulm
                            Yes, it will happen during the loop execution, that is the whole point of my problem.

                            Rather than talking abstract things, this is what I want to do:
                            I have a large plain text file (300k+) which is an airport database in a flight sim.
                            The entries are formatted according to their specs with all entries in plain text to make it easy to read in a text editor if desired.
                            There are over 28000 entries in there.
                            There are some formatting errors also that stop me from processing the data as I wish.
                            I have a program that I wrote in Rad Studio that does exactly what I want.
                            It reads every entry, skips the fields I don't want, collects various statistics and file locations that I do want and gives me the output that I am looking for. While processing, it will print whatever I tell it on the screen in a memo field.
                            I used this to good effect when trying to get around some of the wrongly formatted data that was crashing my program, i.e. finding out where it was crashing, and on which entry.
                            I have got a program that runs error free now and will print out the erroneous entries as they occur so that I can manually edit the file and correct them.

                            I have no problem with Rad Studio, I have been a C++ Builder user for years but it only works on Windows. As the sim runs on MacOS and Linux as well, I wanted to try to write the same program that I could compile on all 3 platforms, hence the use of Qt.

                            JonBJ 1 Reply Last reply
                            0
                            • C Colins2

                              @jsulm
                              Yes, it will happen during the loop execution, that is the whole point of my problem.

                              Rather than talking abstract things, this is what I want to do:
                              I have a large plain text file (300k+) which is an airport database in a flight sim.
                              The entries are formatted according to their specs with all entries in plain text to make it easy to read in a text editor if desired.
                              There are over 28000 entries in there.
                              There are some formatting errors also that stop me from processing the data as I wish.
                              I have a program that I wrote in Rad Studio that does exactly what I want.
                              It reads every entry, skips the fields I don't want, collects various statistics and file locations that I do want and gives me the output that I am looking for. While processing, it will print whatever I tell it on the screen in a memo field.
                              I used this to good effect when trying to get around some of the wrongly formatted data that was crashing my program, i.e. finding out where it was crashing, and on which entry.
                              I have got a program that runs error free now and will print out the erroneous entries as they occur so that I can manually edit the file and correct them.

                              I have no problem with Rad Studio, I have been a C++ Builder user for years but it only works on Windows. As the sim runs on MacOS and Linux as well, I wanted to try to write the same program that I could compile on all 3 platforms, hence the use of Qt.

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

                              @Colins2
                              At the risk of (a) predicting and (b) infuriating @jsulm (who, btw, does know what he is talking about), I believe he will recommend putting your large file reading code into a separate thread, so that it does not block the UI since it takes so long to read/process. Send send signals from that thread to a slot in the main UI thread which updates the memo field. You will see output as you go along.

                              Now, I really hope he does not scream at me, but if you want a rough & ready solution while you await his input, @Gerd's

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

                              right after

                              ui->product->display(temp);

                              will (I think) probably get you by without a separate thread, for this case... !

                              J.HilkJ C 2 Replies Last reply
                              2
                              • JonBJ JonB

                                @Colins2
                                At the risk of (a) predicting and (b) infuriating @jsulm (who, btw, does know what he is talking about), I believe he will recommend putting your large file reading code into a separate thread, so that it does not block the UI since it takes so long to read/process. Send send signals from that thread to a slot in the main UI thread which updates the memo field. You will see output as you go along.

                                Now, I really hope he does not scream at me, but if you want a rough & ready solution while you await his input, @Gerd's

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

                                right after

                                ui->product->display(temp);

                                will (I think) probably get you by without a separate thread, for this case... !

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

                                @JonB
                                There's always a right way and an easy way to do things.

                                alt text


                                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
                                3
                                • JonBJ JonB

                                  @Colins2
                                  At the risk of (a) predicting and (b) infuriating @jsulm (who, btw, does know what he is talking about), I believe he will recommend putting your large file reading code into a separate thread, so that it does not block the UI since it takes so long to read/process. Send send signals from that thread to a slot in the main UI thread which updates the memo field. You will see output as you go along.

                                  Now, I really hope he does not scream at me, but if you want a rough & ready solution while you await his input, @Gerd's

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

                                  right after

                                  ui->product->display(temp);

                                  will (I think) probably get you by without a separate thread, for this case... !

                                  C Offline
                                  C Offline
                                  Colins2
                                  wrote on last edited by
                                  #32

                                  @JonB @J-Hilk

                                  I had thought about it the other way round, i.e. how to create a separate thread for the messages and progress bar but your idea sounds the better way.
                                  Thanks for everyone's input

                                  JonBJ 1 Reply Last reply
                                  0
                                  • C Colins2

                                    @JonB @J-Hilk

                                    I had thought about it the other way round, i.e. how to create a separate thread for the messages and progress bar but your idea sounds the better way.
                                    Thanks for everyone's input

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

                                    @Colins2
                                    In Qt you don't have a choice about this: all UI operations must be performed in the main thread (not sure how that's defined, maybe the one where you call QApplication from), any non-UI can be performed in separate threads, but not the other way round.

                                    Hence in Qt: "All threads are equal, but some are more equal than others" :)

                                    C 1 Reply Last reply
                                    1
                                    • JonBJ JonB

                                      @Colins2
                                      In Qt you don't have a choice about this: all UI operations must be performed in the main thread (not sure how that's defined, maybe the one where you call QApplication from), any non-UI can be performed in separate threads, but not the other way round.

                                      Hence in Qt: "All threads are equal, but some are more equal than others" :)

                                      C Offline
                                      C Offline
                                      Colins2
                                      wrote on last edited by
                                      #34

                                      @JonB
                                      Thanks :-)
                                      Just reading through the Mandelbrot code; that seems to be the sort of thing I want.

                                      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