Initializing QTextStream with Text Edit...
-
wrote on 19 Sept 2019, 02:13 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); }
-
@alexanderalexander said in Initializing QTextStream with Text Edit...:
&this->textEdit_T->toPlainText()
This creates a temporary, so your QTextStream works on a dangling pointer.
-
@alexanderalexander said in Initializing QTextStream with Text Edit...:
&this->textEdit_T->toPlainText()
This creates a temporary, so your QTextStream works on a dangling pointer.
wrote on 19 Sept 2019, 05:22 last edited by@christian-ehrlicher
Then what would be the proper way to create the Text Stream? -
@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.
-
@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.
wrote on 19 Sept 2019, 05:58 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. -
@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?
-
@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?
wrote on 19 Sept 2019, 06:13 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? -
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.
-
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.
wrote on 19 Sept 2019, 06:32 last edited by@christian-ehrlicher
Ok, I understand what you mean now. Thanks for the help m8 -
@alexanderalexander: Then please mark the topic as solved, thx.
1/10