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. Initializing QTextStream with Text Edit...
Forum Update on Monday, May 27th 2025

Initializing QTextStream with Text Edit...

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 955 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.
  • A Offline
    A Offline
    AlexanderAlexander
    wrote on 19 Sept 2019, 02:13 last edited by
    #1

    I am trying to initialize a Q Text Stream with the plain text contents of my plain Text Edit, but am getting very bad results (I have done this successfully when reading from a file, but am confused with the Text Edit).

    QTextStream inT(&this->textEdit_T->toPlainText());
    QStringList dataT;
    QString currLine;
    while (!inT.atEnd()) {
    	currLine = inTime.readLine();
    	dataTime.append(currLine);
    }
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 19 Sept 2019, 05:01 last edited by
      #2

      @alexanderalexander said in Initializing QTextStream with Text Edit...:

      &this->textEdit_T->toPlainText()

      This creates a temporary, so your QTextStream works on a dangling pointer.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      A 1 Reply Last reply 19 Sept 2019, 05:22
      2
      • C Christian Ehrlicher
        19 Sept 2019, 05:01

        @alexanderalexander said in Initializing QTextStream with Text Edit...:

        &this->textEdit_T->toPlainText()

        This creates a temporary, so your QTextStream works on a dangling pointer.

        A Offline
        A Offline
        AlexanderAlexander
        wrote on 19 Sept 2019, 05:22 last edited by
        #3

        @christian-ehrlicher
        Then what would be the proper way to create the Text Stream?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 19 Sept 2019, 05:44 last edited by
          #4

          @alexanderalexander said in Initializing QTextStream with Text Edit...:

          Then what would be the proper way to create the Text Stream?

          Don't use a temporary QString object - store the return value of textEdit_T->toPlainText() in a local variable.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          A 1 Reply Last reply 19 Sept 2019, 05:58
          1
          • C Christian Ehrlicher
            19 Sept 2019, 05:44

            @alexanderalexander said in Initializing QTextStream with Text Edit...:

            Then what would be the proper way to create the Text Stream?

            Don't use a temporary QString object - store the return value of textEdit_T->toPlainText() in a local variable.

            A Offline
            A Offline
            AlexanderAlexander
            wrote on 19 Sept 2019, 05:58 last edited by
            #5

            @christian-ehrlicher
            But I would like to read it like a stream, line by line. If I store it in a local variable I am stuck with just a QString.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 19 Sept 2019, 06:02 last edited by
              #6

              @alexanderalexander said in Initializing QTextStream with Text Edit...:

              But I would like to read it like a stream, line by line.

              I don't understand your problem - why can't you pass the local QString object to QTextStream?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              A 1 Reply Last reply 19 Sept 2019, 06:13
              0
              • C Christian Ehrlicher
                19 Sept 2019, 06:02

                @alexanderalexander said in Initializing QTextStream with Text Edit...:

                But I would like to read it like a stream, line by line.

                I don't understand your problem - why can't you pass the local QString object to QTextStream?

                A Offline
                A Offline
                AlexanderAlexander
                wrote on 19 Sept 2019, 06:13 last edited by
                #7

                @christian-ehrlicher
                If I take the resulting QString from textEdit->toPlainText() and initialize it a Text Stream with it will the endln's persist? Do you know how Text Edits handle wrapping when converting like this?

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 19 Sept 2019, 06:20 last edited by
                  #8

                  I still don't understand your problem. I said you're passing a temporary to QTextStream since "&this->textEdit_T->toPlainText()" only creates a pointer to a temporary QString object. Therefore you should save the result of "textEdit_T->toPlainText()" and use this for your QTextStream.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  A 1 Reply Last reply 19 Sept 2019, 06:32
                  2
                  • C Christian Ehrlicher
                    19 Sept 2019, 06:20

                    I still don't understand your problem. I said you're passing a temporary to QTextStream since "&this->textEdit_T->toPlainText()" only creates a pointer to a temporary QString object. Therefore you should save the result of "textEdit_T->toPlainText()" and use this for your QTextStream.

                    A Offline
                    A Offline
                    AlexanderAlexander
                    wrote on 19 Sept 2019, 06:32 last edited by
                    #9

                    @christian-ehrlicher
                    Ok, I understand what you mean now. Thanks for the help m8

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 19 Sept 2019, 06:39 last edited by
                      #10

                      @alexanderalexander: Then please mark the topic as solved, thx.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      0

                      1/10

                      19 Sept 2019, 02:13

                      • Login

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