QPlainTextEdit removes carriage returns "\r"
-
Hello
It seems that when text is entered to a QPlainTextEdit the carriage returns "\r" are lost.
I wrote a program to test:int main(int argc, char *argv[]) { QApplication a(argc, argv); QPlainTextEdit * text_edit = new QPlainTextEdit(); QString str = "First line"; str.append((QChar)13); str.append((QChar)10); str.append("second line"); qDebug() << str; qDebug() << "original size of string : " << str.size(); text_edit->document()->setPlainText(str); text_edit->show(); QString str_from_text_edit = text_edit->document()->toPlainText(); qDebug() << "size of string from text edit: " << str_from_text_edit.size(); for (int g = 0; g < str.size(); g++) if (str[g] != str_from_text_edit[g]) { qDebug() << "Different at character number " << g; break; } return a.exec(); }
This is a problem for me because i'm working on a search tool and if the carriage returns are lost the positions on the file stream i found before using find() are no longer usable, and i cannot highlight the results in the QPlainTextEdit.
This was discussed here as well:
https://forum.qt.io/topic/42464/qplaintextedit-eats-line-endings/6
the last post there suggest to inherit the class and override the function QTextCursor::insertText , hopefully this won't break anything? -
@amadeok said in QPlainTextEdit removes carriage returns "\r":
the positions on the file stream i found before using find() are no longer usable
What
find()
?You are presumably showing a text file. And opening it as a text file. In C++
\r\n
is reduced to\n
under Windows.QPlainTextEdit
will do the same if presented with\r\n
. You would be best doing whatever searching/finding against the stream as a text file, or against whatever is actually in theQPlainTextEdit
. -
@amadeok said in QPlainTextEdit removes carriage returns "\r":
the positions on the file stream i found before using find()
The discussion here still applies: https://forum.qt.io/topic/129758/display-a-std-string-in-a-qlineedit-in-qt
Use
QString::indexOf()
instead ofstd::find()