Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTText Edit converts '\r\n' to '\n'
Forum Updated to NodeBB v4.3 + New Features

QTText Edit converts '\r\n' to '\n'

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 592 Views 2 Watching
  • 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 Offline
    A Offline
    Ayush Gupta
    wrote on last edited by
    #1

    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

    JKSHJ 1 Reply Last reply
    0
    • A Ayush Gupta

      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

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      @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 using QFile with the QIODevice::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 call QTextStream::setGenerateByteOrderMark() to produce the BOM in your output file. See https://doc.qt.io/qt-5/qtextstream.html#setGenerateByteOrderMark

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      5
      • A Offline
        A Offline
        Ayush Gupta
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          You can read a file byte-for-byte into a QByteArray using QFile opened without QIODevice::Text.

          1 Reply Last reply
          3
          • A Offline
            A Offline
            Ayush Gupta
            wrote on last edited by
            #5

            there is no file all the contents are in memory only.

            JonBJ JKSHJ 2 Replies Last reply
            0
            • A Ayush Gupta

              there is no file all the contents are in memory only.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Ayush-Gupta
              Then you could do it without a QTextStream 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 a QTextStream 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.

              1 Reply Last reply
              0
              • A Ayush Gupta

                there is no file all the contents are in memory only.

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                @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?

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                1
                • S Offline
                  S Offline
                  SimonSchroeder
                  wrote on last edited by
                  #8

                  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 uses QString instead and will thus convert and interpret your bytes. Without interpretation it is impossible to render text. You can try if a QPlainTextEdit 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.

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved