QPlainTextEdit eats line endings?
-
I have a subclass of a QPlainTextEdit.
I watch some text go into it, with the function call:
@ui->editor->setPlainText(text);@
The object there named "editor" is the subclass of the QPlainTextEdit. The setPlainText() function is simply the default QPlaintextEdit function. No changes made to that.I can look at that QString named text and I can see in it windows style line-endings (that is, I can see "\r\n" in it), so the text I'm putting in has windows style line-endings.
When I then summon the contents of the text edit window (to write to disk), using the function toPlaintext() (which I understand is ultimately QTextDocument::toPlainText) the line endings in that are just "\r".
So it looks like the QPlainTextEdit is changing the line-endings from Windows style to *nix style. I'd really rather it didn't. I can't find anything in the QPlainTextEdit or QTextDocument class reference to control this. Is it possible to dictate line-endings so that I get back text with my choice of line-ending style, or am I goig to have to manually go through the returned QString from toPlaintext() and add the "\r" back in myself before every "\n"?
-
Hi, indeed the QString you receive from QTextDocument::toPlainText() does not have CRLF pairs.
When you write or stream it to a disk file, for example by using a QFile, if you add "QIODevice::Text" to the mode in QFile::open() your file will have correct CRLF pairs. Read more "here":http://qt-project.org/doc/qt-5/qfile.html#open-2
(look for Note for the Windows platform) -
It seems to write correct line endings to file on the assumption that the "correct" line endings are whatever the local operating system typically uses. I've got round it by manually entering an appropriate line ending at various places into the QString (either "\n" or "\r\n") and writing out as data rather than text. Seems to work so far...
-
Can you share your solution. I'm facing same problem.
@
//text contains \r\n
if(!preserveUndo)
{
setContentChangesSignal(false);
setPlainText(text);setContentChangesSignal(true); if(isMinimap) miniMap->m_SetSource(text); } else { QTextCursor tc = textCursor(); tc.beginEditBlock(); int position = tc.position(); tc.select(QTextCursor::Document); tc.insertText(text); tc.setPosition(position); tc.endEditBlock(); //setTextCursor(tc); }
// Now lost
QString n = toPlainText(); //n doesn't contain \r\n only \nI'm working on linux environment and save file in windows line ending style.
@ -
I did it as follows:
On opening the file, look through it for line endings and establish if it is linux or windows style. Remember that.
When saving the file, if it is meant to be windows style, get all the text from the QPlainTextEdit, manually go through it finding every linux-style line ending with a windows-style one
@ if (lineEndingType == WINDOWS)
{
Text.replace("\n", "\r\n");
Text.replace("\r\r\n", "\r\n");
}@and then save it as data (not text), i.e. open the output file WITHOUT the flag QFile::Text
NOT this:
@ QFile result(fileName);
if(result.open(QFile::WriteOnly | QFile::Text))
{...@but this:
@ QFile result(fileName);
if(result.open(QFile::WriteOnly))
{...@ -
Thanks for sharing. ok for saving we have reopen file again. That's like workaround.
I found main problem in Qtextcursor
https://qt.gitorious.org/qt/qt/source/6ae84f1183e91c910ca92a55e37f8254ace805c0:src/gui/text/qtextcursor.cpp#L1424@ if (ch == QLatin1Char('\r')
&& (i + 1) < text.length()
&& text.at(i + 1) == QLatin1Char('\n')) {
++i;
ch = text.at(i);
}@We are ignoring /r in this loop.
Either inherit Qtextcursor and change this behavior.
or may be some other way we can change this. I don't know.