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

Initializing QTextStream with Text Edit...

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 1.1k 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 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
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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
      2
      • Christian EhrlicherC Christian Ehrlicher

        @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 last edited by
        #3

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

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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
          1
          • Christian EhrlicherC Christian Ehrlicher

            @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 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
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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
              0
              • Christian EhrlicherC Christian Ehrlicher

                @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 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
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 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
                  2
                  • Christian EhrlicherC Christian Ehrlicher

                    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 last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 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

                      • Login

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