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. Printing file content (text) on a text browser
Servers for Qt installer are currently down

Printing file content (text) on a text browser

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.3k 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 Offline
    R Offline
    russjohn834
    wrote on 2 Jun 2021, 14:44 last edited by
    #1

    Hi,

    I've a basic question. I do the follwoing to display file content in a textBrowser:

     QString name = "log_file_"+pLabel+".txt";
    
        QFile file(name);
    
        file.open(QIODevice::ReadOnly|QIODevice::Text);
    
        QTextStream instream(& file);
    
        while (!instream.atEnd()){
                QString line = instream.readLine();
    
                qDebug()<<line; // I can see all the lines here 
    
                ui->textBrowser->setText(line+"\n"); // Here only the last line is printed 
            }
    
    
         file.close();
    

    in the above snipet

    ui->textBrowser->setText(line+"\n"); /
    

    Only prints the last line of the text, whereas I need to print whole contents on the text on textBrowser. Can you spot a mistake here?

    A 1 Reply Last reply 2 Jun 2021, 15:02
    0
    • R russjohn834
      2 Jun 2021, 14:44

      Hi,

      I've a basic question. I do the follwoing to display file content in a textBrowser:

       QString name = "log_file_"+pLabel+".txt";
      
          QFile file(name);
      
          file.open(QIODevice::ReadOnly|QIODevice::Text);
      
          QTextStream instream(& file);
      
          while (!instream.atEnd()){
                  QString line = instream.readLine();
      
                  qDebug()<<line; // I can see all the lines here 
      
                  ui->textBrowser->setText(line+"\n"); // Here only the last line is printed 
              }
      
      
           file.close();
      

      in the above snipet

      ui->textBrowser->setText(line+"\n"); /
      

      Only prints the last line of the text, whereas I need to print whole contents on the text on textBrowser. Can you spot a mistake here?

      A Offline
      A Offline
      artwaw
      wrote on 2 Jun 2021, 15:02 last edited by artwaw 6 Feb 2021, 15:03
      #2

      @russjohn834 Hi,
      please look more closely at your code.

      ui->textBrowser->setText(line+"\n");
      

      You call setText() with a single line only.
      You could either use setSource() or read the whole content of the file and then setText().

      I imagine using setSource() would eliminate the need for the QFile and related.

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      3
      • V Offline
        V Offline
        VRonin
        wrote on 2 Jun 2021, 15:48 last edited by
        #3

        @artwaw is correct.
        An alternative is to use append instead of setText

        "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

        R 1 Reply Last reply 2 Jun 2021, 15:58
        3
        • R Offline
          R Offline
          russjohn834
          wrote on 2 Jun 2021, 15:56 last edited by
          #4

          @artwaw Thanks a lot for pointing it out. I replaced readLine() with readAll(), and it works!

          1 Reply Last reply
          1
          • V VRonin
            2 Jun 2021, 15:48

            @artwaw is correct.
            An alternative is to use append instead of setText

            R Offline
            R Offline
            russjohn834
            wrote on 2 Jun 2021, 15:58 last edited by
            #5

            @VRonin Thank you!, that also works :)

            1 Reply Last reply
            0

            1/5

            2 Jun 2021, 14:44

            • Login

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