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. Print out a txt file to text box
QtWS25 Last Chance

Print out a txt file to text box

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 12.4k 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.
  • M Offline
    M Offline
    MFreeM
    wrote on last edited by
    #1

    Hey there, another newbie question....

    I would like to display the contents of a text file on screen. Not in console, though.

    I can read in the file using QFile and hold the contents in QString. But i want to be able to see it on screen, so perhaps print it to a textbox.

    Does anybody know how or what class i would use to do this?

    Thanks in advance.

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      Example code :
      @QString str;
      QTextEdit *fileText;

      fileText->append(str)); @

      you could use a QLabel also...depending if you want to be able to edit.

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MFreeM
        wrote on last edited by
        #3

        Thanks Eddy, I'll have a play.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mariusmssj
          wrote on last edited by
          #4

          Sorry to be a total noob but what I am trying to do is to load in the content from a .txt file that will contain code and display it in my program.

          And I am not sure sure what kind of textbox should I use because I want to keep all the tabs and code structure, also with the example by Eddy how would I put that content into the text box?

          Thank you

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            A [[Doc:QTextBrowser]] with a fixed size font would be a good start. To set the contents of a file to the text box, you can use:

            @
            QFile file("/path/to/the/file.txt");
            file.open(QIODevice::ReadOnly);
            QTextStream stream(&file);
            QString content = stream.readAll();
            file.close();
            textBrowser.setPlainText(content);
            @

            You will have to add error checking (file open may fail).

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mariusmssj
              wrote on last edited by
              #6

              Thank you Volker it worked wonders, I got another question if you don't mind.
              How would I load in another .txt file into the same text box just below the text that I loaded in from the precious file? Because I want to combine multiple text files into one textbox.
              Thank you

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                In that case just replace setPlainText(content) with append(content).

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mariusmssj
                  wrote on last edited by
                  #8

                  Once again Volker thank you very much for your help!

                  1 Reply Last reply
                  0

                  • Login

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