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. Read line by line in a specific time interval
QtWS25 Last Chance

Read line by line in a specific time interval

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 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.
  • S Offline
    S Offline
    Shahmisufi
    wrote on last edited by
    #1

    Hi guys,

    I am trying to transfer the content of a text file to be displayed to the textEdit widget inside my GUI.

    How can I make it display at the text edit one line at a time, instead of displaying all the content of the text file into the textEdit widget at one time?

    By using "readLine" it can only display the first line of the text file content. How can I make it display the second line of the content after, let say 2 second?

    I am a new guy learning Qt Programming, so please be gentle to me~~ ^^

    Venkatesh VV 1 Reply Last reply
    0
    • S Shahmisufi

      Hi guys,

      I am trying to transfer the content of a text file to be displayed to the textEdit widget inside my GUI.

      How can I make it display at the text edit one line at a time, instead of displaying all the content of the text file into the textEdit widget at one time?

      By using "readLine" it can only display the first line of the text file content. How can I make it display the second line of the content after, let say 2 second?

      I am a new guy learning Qt Programming, so please be gentle to me~~ ^^

      Venkatesh VV Offline
      Venkatesh VV Offline
      Venkatesh V
      wrote on last edited by
      #2

      @Shahmisufi
      Hi...

      Try this,

      QFile inputFile(fileName);
      if (inputFile.open(QIODevice::ReadOnly))
      {
      QTextStream in(&inputFile);
      while (!in.atEnd())
      {
      QString line = in.readLine();
      }
      inputFile.close();
      }

      S 1 Reply Last reply
      3
      • Venkatesh VV Venkatesh V

        @Shahmisufi
        Hi...

        Try this,

        QFile inputFile(fileName);
        if (inputFile.open(QIODevice::ReadOnly))
        {
        QTextStream in(&inputFile);
        while (!in.atEnd())
        {
        QString line = in.readLine();
        }
        inputFile.close();
        }

        S Offline
        S Offline
        Shahmisufi
        wrote on last edited by
        #3

        @Venkatesh-V thank you...

        I have tried the code, but it will not display at the textEdit widget one at a time. Instead, it will read it line by line and display it inside the textedit widget all in one time.

        It does not able to display line by line between intervals

        Venkatesh VV 2 Replies Last reply
        0
        • S Shahmisufi

          @Venkatesh-V thank you...

          I have tried the code, but it will not display at the textEdit widget one at a time. Instead, it will read it line by line and display it inside the textedit widget all in one time.

          It does not able to display line by line between intervals

          Venkatesh VV Offline
          Venkatesh VV Offline
          Venkatesh V
          wrote on last edited by
          #4

          @Shahmisufi

          For that you need to store all the lines in QList. after that take a Qtimer instance set it 2 second interval and start the timer, once the timer emits timeout connect to another slot there you can take one value at a time from the list and set to textedit.

          Venkatesh VV 1 Reply Last reply
          2
          • Venkatesh VV Venkatesh V

            @Shahmisufi

            For that you need to store all the lines in QList. after that take a Qtimer instance set it 2 second interval and start the timer, once the timer emits timeout connect to another slot there you can take one value at a time from the list and set to textedit.

            Venkatesh VV Offline
            Venkatesh VV Offline
            Venkatesh V
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • S Shahmisufi

              @Venkatesh-V thank you...

              I have tried the code, but it will not display at the textEdit widget one at a time. Instead, it will read it line by line and display it inside the textedit widget all in one time.

              It does not able to display line by line between intervals

              Venkatesh VV Offline
              Venkatesh VV Offline
              Venkatesh V
              wrote on last edited by VRonin
              #6

              @Shahmisufi

              @Shahmisufi

              //Header File
              
              QList<QString> LineList;
              int count = 0;
              
              
              //SourceFile
              {
              QFile inputFile(fileName);
              if (inputFile.open(QIODevice::ReadOnly))
              {
              QTextStream in(&inputFile);
              while (!in.atEnd())
              {
              QString line = in.readLine();
              LineList.apppend(line);
              }
              inputFile.close();
              }
              
              QTimer *timer = new QTimer;
              timer->setInterval(2000);
              timer->start();
              
              connect(timer,SIGNAL(timeOut(),this,SLOT(readLine()));
              }
              
              
              
              void readLine()
              {
              textEdit->setText(textEdit->text()+LineList.at(count));
              count++;
              }
              
              S 3 Replies Last reply
              5
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                Alternative, less memory hungry but needs the file not to be deleted until the process finished:

                QFile* inputFile=new QFile(fileName,textEdit);
                if (!inputFile.open(QIODevice::ReadOnly)){
                inputFile->deleteLater();
                return;
                }
                QTimer* inputTimer= new QTimer(textEdit); 
                auto readSingleLine = [=]()->void{
                QTextStream in(inputFile);
                if(in.atEnd()){
                inputFile->deleteLater();
                inputTimer->deleteLater();
                return;
                }
                const QString line = in.readLine();
                textEdit->moveCursor(QTextCursor::End);
                textEdit->textCursor().insertText(line); //not sure if you need to manually add the newline here
                };
                QObject::connect(inputTimer,&QTimer::timeout,textEdit,readSingleLine);
                readSingleLine();
                inputTimer->start(2000); //2 seconds
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                3
                • Venkatesh VV Venkatesh V

                  @Shahmisufi

                  @Shahmisufi

                  //Header File
                  
                  QList<QString> LineList;
                  int count = 0;
                  
                  
                  //SourceFile
                  {
                  QFile inputFile(fileName);
                  if (inputFile.open(QIODevice::ReadOnly))
                  {
                  QTextStream in(&inputFile);
                  while (!in.atEnd())
                  {
                  QString line = in.readLine();
                  LineList.apppend(line);
                  }
                  inputFile.close();
                  }
                  
                  QTimer *timer = new QTimer;
                  timer->setInterval(2000);
                  timer->start();
                  
                  connect(timer,SIGNAL(timeOut(),this,SLOT(readLine()));
                  }
                  
                  
                  
                  void readLine()
                  {
                  textEdit->setText(textEdit->text()+LineList.at(count));
                  count++;
                  }
                  
                  S Offline
                  S Offline
                  Shahmisufi
                  wrote on last edited by
                  #8

                  @Venkatesh-V , thank you. will update to you once I've run the program

                  1 Reply Last reply
                  1
                  • Venkatesh VV Venkatesh V

                    @Shahmisufi

                    @Shahmisufi

                    //Header File
                    
                    QList<QString> LineList;
                    int count = 0;
                    
                    
                    //SourceFile
                    {
                    QFile inputFile(fileName);
                    if (inputFile.open(QIODevice::ReadOnly))
                    {
                    QTextStream in(&inputFile);
                    while (!in.atEnd())
                    {
                    QString line = in.readLine();
                    LineList.apppend(line);
                    }
                    inputFile.close();
                    }
                    
                    QTimer *timer = new QTimer;
                    timer->setInterval(2000);
                    timer->start();
                    
                    connect(timer,SIGNAL(timeOut(),this,SLOT(readLine()));
                    }
                    
                    
                    
                    void readLine()
                    {
                    textEdit->setText(textEdit->text()+LineList.at(count));
                    count++;
                    }
                    
                    S Offline
                    S Offline
                    Shahmisufi
                    wrote on last edited by
                    #9

                    @Venkatesh-V Dear sir I have tried your code, but when I run i the issue highlighted is

                    " 'Class QTextEdit' has no member named 'text' "

                    for the code line below :

                    textEdit->setText(textEdit->text()+LineList.at(count));
                    
                    m.sueM Venkatesh VV 2 Replies Last reply
                    0
                    • S Shahmisufi

                      @Venkatesh-V Dear sir I have tried your code, but when I run i the issue highlighted is

                      " 'Class QTextEdit' has no member named 'text' "

                      for the code line below :

                      textEdit->setText(textEdit->text()+LineList.at(count));
                      
                      m.sueM Offline
                      m.sueM Offline
                      m.sue
                      wrote on last edited by
                      #10

                      @Shahmisufi

                      Hi,

                      instead of text() just use the function toPlainText().

                      -Michael.

                      1 Reply Last reply
                      3
                      • S Shahmisufi

                        @Venkatesh-V Dear sir I have tried your code, but when I run i the issue highlighted is

                        " 'Class QTextEdit' has no member named 'text' "

                        for the code line below :

                        textEdit->setText(textEdit->text()+LineList.at(count));
                        
                        Venkatesh VV Offline
                        Venkatesh VV Offline
                        Venkatesh V
                        wrote on last edited by
                        #11

                        @Shahmisufi

                        Hi,

                        Sorry, as @m-sue said use toPlainText() method to get current text of textEdit. this will resolve your query.

                        1 Reply Last reply
                        2
                        • Venkatesh VV Venkatesh V

                          @Shahmisufi

                          @Shahmisufi

                          //Header File
                          
                          QList<QString> LineList;
                          int count = 0;
                          
                          
                          //SourceFile
                          {
                          QFile inputFile(fileName);
                          if (inputFile.open(QIODevice::ReadOnly))
                          {
                          QTextStream in(&inputFile);
                          while (!in.atEnd())
                          {
                          QString line = in.readLine();
                          LineList.apppend(line);
                          }
                          inputFile.close();
                          }
                          
                          QTimer *timer = new QTimer;
                          timer->setInterval(2000);
                          timer->start();
                          
                          connect(timer,SIGNAL(timeOut(),this,SLOT(readLine()));
                          }
                          
                          
                          
                          void readLine()
                          {
                          textEdit->setText(textEdit->text()+LineList.at(count));
                          count++;
                          }
                          
                          S Offline
                          S Offline
                          Shahmisufi
                          wrote on last edited by
                          #12

                          @Venkatesh-V dear sir, I have some error at this line

                          void readLine()
                          {
                            textEdit->setText(textEdit->toPlainText()+LineList.at(count));
                            count++;
                          }
                          
                          1. 'textEdit' was not declared in this scope
                          2. 'LineList' was not declared in this scope'count' was not declared in this scope
                          3. 'count' was not declared in this scope

                          So, i have changed from

                          void readLine()
                          

                          to

                          void Speedometer::readLine()
                          
                          

                          hence, its executable, but then it doesn't respond as we wanted. It does not display any text inside the TextEdit widget.

                          Venkatesh VV 1 Reply Last reply
                          1
                          • S Shahmisufi

                            @Venkatesh-V dear sir, I have some error at this line

                            void readLine()
                            {
                              textEdit->setText(textEdit->toPlainText()+LineList.at(count));
                              count++;
                            }
                            
                            1. 'textEdit' was not declared in this scope
                            2. 'LineList' was not declared in this scope'count' was not declared in this scope
                            3. 'count' was not declared in this scope

                            So, i have changed from

                            void readLine()
                            

                            to

                            void Speedometer::readLine()
                            
                            

                            hence, its executable, but then it doesn't respond as we wanted. It does not display any text inside the TextEdit widget.

                            Venkatesh VV Offline
                            Venkatesh VV Offline
                            Venkatesh V
                            wrote on last edited by
                            #13

                            @Shahmisufi

                            Hi,
                            have you declared readLine() in public slots section?

                            //In Header File

                            public slots:
                            void readLine();

                            1 Reply Last reply
                            4

                            • Login

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