Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. Hi, I am new for Qt, i am trying to display 0 to 999 in text Browser but when i am executing following code it is directly showing 999 output can anyone help me, thank you in advance
Forum Updated to NodeBB v4.3 + New Features

Hi, I am new for Qt, i am trying to display 0 to 999 in text Browser but when i am executing following code it is directly showing 999 output can anyone help me, thank you in advance

Scheduled Pinned Locked Moved Unsolved Installation and Deployment
qtcreatorproblempogram
15 Posts 6 Posters 3.1k 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.
  • R Rameshwar
    for(j=0;j<1000;j++)
    //     {
             ui->textBrowser->setText(QString::number(j));   //display 0 to 999
    //     }
    //your code here
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #2

    @Rameshwar
    First, my friend, kindly think about the title of any thread you start here. It should be a couple of words summary, e.g. Problem displaying text, rather than a whole sentence.

    https://doc.qt.io/qt-5/qtextedit.html#setText (inherited by your QTextBrowser) sets the complete text, overwriting whatever was there. So your code runs through and by the end all your see is the final number, 999. Also, because you do not allow the Qt "event loop" to be called in your loop, you wouldn't actually see the earlier numbers displayed anyway, you only see the final 999 because only after your loop does Qt get to actually draw it on the screen.

    Either you mean to put some kind of delay in between each number and allow Qt to re-paint the widget (which you would probably do with timers, signals & slots, which might be a bit advanced for what you have in mind), or perhaps you'd just like to append the next number each time:

    ui->textBrowser->setText(ui->textBrowser->text() + ", " + QString::number(j)); 
    

    or (slightly different):

    ui->textBrowser->append(QString::number(j)); 
    

    You will still only get to see the final state, but at least all the numbers will be there.

    R 1 Reply Last reply
    6
    • JonBJ JonB

      @Rameshwar
      First, my friend, kindly think about the title of any thread you start here. It should be a couple of words summary, e.g. Problem displaying text, rather than a whole sentence.

      https://doc.qt.io/qt-5/qtextedit.html#setText (inherited by your QTextBrowser) sets the complete text, overwriting whatever was there. So your code runs through and by the end all your see is the final number, 999. Also, because you do not allow the Qt "event loop" to be called in your loop, you wouldn't actually see the earlier numbers displayed anyway, you only see the final 999 because only after your loop does Qt get to actually draw it on the screen.

      Either you mean to put some kind of delay in between each number and allow Qt to re-paint the widget (which you would probably do with timers, signals & slots, which might be a bit advanced for what you have in mind), or perhaps you'd just like to append the next number each time:

      ui->textBrowser->setText(ui->textBrowser->text() + ", " + QString::number(j)); 
      

      or (slightly different):

      ui->textBrowser->append(QString::number(j)); 
      

      You will still only get to see the final state, but at least all the numbers will be there.

      R Offline
      R Offline
      Rameshwar
      wrote on last edited by
      #3

      @JonB
      But I want to display 0 and then 1 and then 2.....999 on window. you solve my problem almost but it only display after execution of totaly for loop. but i want display "1" then add 1 and display "2" and then add 1 and display "3" like this.

      J.HilkJ 1 Reply Last reply
      0
      • R Rameshwar

        @JonB
        But I want to display 0 and then 1 and then 2.....999 on window. you solve my problem almost but it only display after execution of totaly for loop. but i want display "1" then add 1 and display "2" and then add 1 and display "3" like this.

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

        @Rameshwar like @JonB said, that is possible, but will look complicated

        #include <QApplication>
        #include <QTextBrowser>
        #include <QTimer>
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
            QTextBrowser *browser = new QTextBrowser();
            browser->setPlainText("0");
            browser->show();
            QTimer *t = new QTimer();
        
            int count = 0;
        
            QObject::connect(t, &QTimer::timeout, browser, [&count, t, browser]()->void{
                if(++count < 100){
                    browser->append(QString::number(count));
                } else {
                    t->stop();
                }
            });
        
            t->start(1000);
        
            return  a.exec();
        }
        

        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.

        R 1 Reply Last reply
        4
        • J.HilkJ J.Hilk

          @Rameshwar like @JonB said, that is possible, but will look complicated

          #include <QApplication>
          #include <QTextBrowser>
          #include <QTimer>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
          
              QTextBrowser *browser = new QTextBrowser();
              browser->setPlainText("0");
              browser->show();
              QTimer *t = new QTimer();
          
              int count = 0;
          
              QObject::connect(t, &QTimer::timeout, browser, [&count, t, browser]()->void{
                  if(++count < 100){
                      browser->append(QString::number(count));
                  } else {
                      t->stop();
                  }
              });
          
              t->start(1000);
          
              return  a.exec();
          }
          
          R Offline
          R Offline
          Rameshwar
          wrote on last edited by
          #5

          @J.Hilk
          but this type not usefull on ui(user interface) window, i want to display this data on mainwindow.ui.

          jsulmJ 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #6

            Hi,

            How is it not useful ? The QTimer approach is the right one if you want to display a progressive counter in a way that your application user can see it.

            Using a loop like you do just blocks the event loop. That's why you only see the widget content after the loop ended.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • R Rameshwar

              @J.Hilk
              but this type not usefull on ui(user interface) window, i want to display this data on mainwindow.ui.

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

              @Rameshwar If you need this code inside mainwindow then put it there, it isn't that hard.

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

              R 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Rameshwar If you need this code inside mainwindow then put it there, it isn't that hard.

                R Offline
                R Offline
                Rameshwar
                wrote on last edited by
                #8

                @jsulm
                but if you write code inside mainwindow then , it does not display step by step directly it displays last value that is 999 or using append it displays 0,1,2.....999 once time but i want it displays step by step first it display "1" then increment (i++) then it display "2" then and again it (i++) increment by one and display 3....999.

                jsulmJ 1 Reply Last reply
                0
                • R Rameshwar

                  @jsulm
                  but if you write code inside mainwindow then , it does not display step by step directly it displays last value that is 999 or using append it displays 0,1,2.....999 once time but i want it displays step by step first it display "1" then increment (i++) then it display "2" then and again it (i++) increment by one and display 3....999.

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

                  @Rameshwar Then you're doing it wrong. Please show your code.

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

                  R 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Rameshwar Then you're doing it wrong. Please show your code.

                    R Offline
                    R Offline
                    Rameshwar
                    wrote on last edited by
                    #10

                    @jsulm

                    for(j=0;j<1000;j++)
                    // {
                    ui->textBrowser->setText(QString::number(j)); //display 0 to 999
                    // }
                    //your code here

                    jsulmJ 1 Reply Last reply
                    0
                    • R Rameshwar

                      @jsulm

                      for(j=0;j<1000;j++)
                      // {
                      ui->textBrowser->setText(QString::number(j)); //display 0 to 999
                      // }
                      //your code here

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

                      @Rameshwar This code is NOT what @J-Hilk suggested...

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

                      R 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @Rameshwar This code is NOT what @J-Hilk suggested...

                        R Offline
                        R Offline
                        Rameshwar
                        wrote on last edited by
                        #12

                        @jsulm i want to display contineousaly some data after one second on window.ui

                        jsulmJ ODБOïO JonBJ 3 Replies Last reply
                        -1
                        • R Rameshwar

                          @jsulm i want to display contineousaly some data after one second on window.ui

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

                          @Rameshwar This is exactly what was already suggested (using QTimer). Did you try?!

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

                          1 Reply Last reply
                          0
                          • R Rameshwar

                            @jsulm i want to display contineousaly some data after one second on window.ui

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

                            @Rameshwar said in Hi, I am new for Qt, i am trying to display 0 to 999 in text Browser but when i am executing following code it is directly showing 999 output can anyone help me, thank you in advance:

                            i want to display contineousaly some data after one second on window.ui

                            everyone here understood your requirement! Solutions are suggested ! why don't you try instead of repeating the same thing over &over ?

                            1 Reply Last reply
                            2
                            • R Rameshwar

                              @jsulm i want to display contineousaly some data after one second on window.ui

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

                              @Rameshwar

                              • @J-Hilk has given you the necessary code to start from. That's why people type in answers to questions. First try that and verify it works.

                              • Then change it so that it writes on your mainwindow or whatever you want instead.

                              • Only after that, write back if you can't get what you want working.

                              1 Reply Last reply
                              2

                              • Login

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