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. QPlainTextEdit eats line endings?
Forum Updated to NodeBB v4.3 + New Features

QPlainTextEdit eats line endings?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 6.0k Views 1 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.
  • M Offline
    M Offline
    Moschops
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      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)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Moschops
        wrote on last edited by
        #3

        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...

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          Yash
          wrote on last edited by
          #4

          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 \n

          I'm working on linux environment and save file in windows line ending style.
          @

          http://kineticwing.com : Web IDE, QSS Editor
          http://speedovation.com : Development Lab

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Moschops
            wrote on last edited by
            #5

            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))
            {...@

            1 Reply Last reply
            0
            • Y Offline
              Y Offline
              Yash
              wrote on last edited by
              #6

              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.

              http://kineticwing.com : Web IDE, QSS Editor
              http://speedovation.com : Development Lab

              1 Reply Last reply
              0

              • Login

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