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
Forum Updated to NodeBB v4.3 + New Features

Read line by line in a specific time interval

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 Posters 4.0k 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.
  • S Offline
    S Offline
    Shahmisufi
    wrote on 2 Jun 2017, 01:59 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~~ ^^

    V 1 Reply Last reply 2 Jun 2017, 03:42
    0
    • S Shahmisufi
      2 Jun 2017, 01:59

      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~~ ^^

      V Offline
      V Offline
      Venkatesh V
      wrote on 2 Jun 2017, 03:42 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 2 Jun 2017, 04:05
      3
      • V Venkatesh V
        2 Jun 2017, 03:42

        @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 2 Jun 2017, 04:05 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

        V 2 Replies Last reply 2 Jun 2017, 04:14
        0
        • S Shahmisufi
          2 Jun 2017, 04:05

          @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

          V Offline
          V Offline
          Venkatesh V
          wrote on 2 Jun 2017, 04:14 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.

          V 1 Reply Last reply 2 Jun 2017, 05:59
          2
          • V Venkatesh V
            2 Jun 2017, 04:14

            @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.

            V Offline
            V Offline
            Venkatesh V
            wrote on 2 Jun 2017, 05:59 last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • S Shahmisufi
              2 Jun 2017, 04:05

              @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

              V Offline
              V Offline
              Venkatesh V
              wrote on 2 Jun 2017, 06:02 last edited by VRonin 6 Feb 2017, 07:06
              #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 Jun 2017, 07:33
              5
              • V Offline
                V Offline
                VRonin
                wrote on 2 Jun 2017, 07:16 last edited by VRonin 6 Mar 2017, 10:53
                #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
                • V Venkatesh V
                  2 Jun 2017, 06:02

                  @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 5 Jun 2017, 07:33 last edited by
                  #8

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

                  1 Reply Last reply
                  1
                  • V Venkatesh V
                    2 Jun 2017, 06:02

                    @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 5 Jun 2017, 08:05 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 V 2 Replies Last reply 5 Jun 2017, 08:18
                    0
                    • S Shahmisufi
                      5 Jun 2017, 08:05

                      @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 5 Jun 2017, 08:18 last edited by
                      #10

                      @Shahmisufi

                      Hi,

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

                      -Michael.

                      1 Reply Last reply
                      3
                      • S Shahmisufi
                        5 Jun 2017, 08:05

                        @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));
                        
                        V Offline
                        V Offline
                        Venkatesh V
                        wrote on 5 Jun 2017, 09:11 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
                        • V Venkatesh V
                          2 Jun 2017, 06:02

                          @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 6 Jun 2017, 02:50 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.

                          V 1 Reply Last reply 6 Jun 2017, 03:56
                          1
                          • S Shahmisufi
                            6 Jun 2017, 02:50

                            @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.

                            V Offline
                            V Offline
                            Venkatesh V
                            wrote on 6 Jun 2017, 03:56 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

                            4/13

                            2 Jun 2017, 04:14

                            9 unread
                            • Login

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