Unsolved QTText Edit converts '\r\n' to '\n'
-
Hi I am trying to edit some file contents using QText Edit. But it changes the contents and replace '\r\n' to '\n' that is from Windows like formatiing to Unix like formatting.
Also Byte Order Mask is changed due to this when I tried to write the contents in file.
Kindly advised how I can keep the byte order mask and format '\r\n' while getting data from QTextEdit
-
@Ayush-Gupta said in QTText Edit converts '\r\n' to '\n':
But it changes the contents and replace '\r\n' to '\n' that is from Windows like formatiing to Unix like formatting.
This is expected.
In Qt, all strings use '\n' in memory, no matter what OS you have. This allows you to write cross-platform code easily without having to worry about line ending formats.
They will be converted back to "\r\n" when you write a text file in Windows (for example, when you write using
QTextStream
or usingQFile
with theQIODevice::Text
flag)Also Byte Order Mask is changed due to this when I tried to write the contents in file.
If you use
QTextStream
to write your file, you can callQTextStream::setGenerateByteOrderMark()
to produce the BOM in your output file. See https://doc.qt.io/qt-5/qtextstream.html#setGenerateByteOrderMark -
Is there way I can get the contents the same contents in memory?
For example i can get the contents in string class variable ? with \r and \n format without using file ? including Byte order mask -
You can read a file byte-for-byte into a QByteArray using QFile opened without QIODevice::Text.
-
there is no file all the contents are in memory only.
-
@Ayush-Gupta
Then you could do it without aQTextStream
at all, and just search in-memory. The trouble is once you start changing content, assuming that alters the length of lines, you will have a problem as you cannot do it in-place. You'd have to copy to a new memory location,. changing as you go. Which is not far off what aQTextStream
solution is doing.TBH I don't know what your whole problem with
QTextEdit
/QTextStream
is.\r\n
gets changes to\n
on reading, but\n
gets changed back to\r\n
on writing, under Windows. -
@Ayush-Gupta said in QTText Edit converts '\r\n' to '\n':
there is no file all the contents are in memory only.
Please explain your use-case. What is the purpose of having a BOM and "\r\n" in memory?
-
Usually, you want to write portable code in C++. Windows has \r\n, Linux has \n and macOS has (or at least used to have) \r. It is much easier to write code to handle just one sort of line breaks. This is why when you read a text file line endings get converted in C++. This is even when you don't use Qt. And it is a good thing that Qt behaves the same.
The trick to not have your file handles/streams convert line endings (and possibly also the BOM) is to open files as binary files instead of text files. Binary files gives you the bits that are store on disk. Also note that Qt will always convert all characters to an internal Unicode representation. This finally means that you will never get the same bits in memory when you handle text as text. Your best bet is to use
QByteArray
to represent the individual bytes in memory. However, there is no default widget to display byte arrays. Every widget usesQString
instead and will thus convert and interpret your bytes. Without interpretation it is impossible to render text. You can try if aQPlainTextEdit
is more suitable to your task. But I doubt that it will solve your problem.Maybe try describing what you are actually trying to achieve and we can help answering if you are actually on the right track.