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 removes carriage returns "\r"
Qt 6.11 is out! See what's new in the release blog

QPlainTextEdit removes carriage returns "\r"

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.5k 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.
  • A Offline
    A Offline
    amadeok
    wrote on last edited by
    #1

    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?

    JonBJ JKSHJ 2 Replies Last reply
    0
    • A amadeok

      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?

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

      @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 the QPlainTextEdit.

      1 Reply Last reply
      0
      • A amadeok

        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?

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

        @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 of std::find()

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

        A 1 Reply Last reply
        2
        • JKSHJ JKSH

          @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 of std::find()

          A Offline
          A Offline
          amadeok
          wrote on last edited by
          #4

          @JonB
          Hi thanks for replies. I was parsing the text from Apache Tika , and so the \r was not removed. calling text.replace("\r\n", "\n"); before doing the search was a good workaround

          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