Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Initializing QTextStream with Text Edit...

    General and Desktop
    2
    10
    440
    Loading More Posts
    • 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
      AlexanderAlexander last edited by

      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 Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        @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 has to stay free or it will die.

        A 1 Reply Last reply Reply Quote 2
        • A
          AlexanderAlexander @Christian Ehrlicher last edited by

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

          1 Reply Last reply Reply Quote 0
          • Christian Ehrlicher
            Christian Ehrlicher Lifetime Qt Champion last edited by

            @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 has to stay free or it will die.

            A 1 Reply Last reply Reply Quote 1
            • A
              AlexanderAlexander @Christian Ehrlicher last edited by

              @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 Reply Quote 0
              • Christian Ehrlicher
                Christian Ehrlicher Lifetime Qt Champion last edited by

                @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 has to stay free or it will die.

                A 1 Reply Last reply Reply Quote 0
                • A
                  AlexanderAlexander @Christian Ehrlicher last edited by

                  @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 Reply Quote 0
                  • Christian Ehrlicher
                    Christian Ehrlicher Lifetime Qt Champion last edited by

                    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 has to stay free or it will die.

                    A 1 Reply Last reply Reply Quote 2
                    • A
                      AlexanderAlexander @Christian Ehrlicher last edited by

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

                      1 Reply Last reply Reply Quote 0
                      • Christian Ehrlicher
                        Christian Ehrlicher Lifetime Qt Champion last edited by

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

                        Qt has to stay free or it will die.

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post