I am currently trying unsuccessfully to find the newline character via the search in a QTextEdit / QPlainTextEdit, everything I have tried is not found. This concerns the search via QTextEdit->find() as well as QTextEdit->document()->find().

The only thing that works is the search via QRegularExpression("$"), but here you get the position of the end of line as a result and must be manually fixed as a workaround. Is there a way to find the newline character?

Here is an example of what I have tried so far and it does not work. As result everywhere -1 is thrown out, exept QRegularExpression( "$" ) that gives as selectionStart() / selectionEnd position 4.


Qt Code:
  1. QTextEdit *edit = new QTextEdit( this );
  2. QPlainTextEdit *plainedit = new QPlainTextEdit( this );
  3.  
  4. QString Text = "Text\nwith\nnewlines\n\n";
  5. edit->setText( Text );
  6. plainedit->setPlainText( Text );
  7.  
  8. qDebug() << edit->document()->find( "\n" ).position();
  9. qDebug() << edit->document()->find( "\r\n" ).position();
  10. qDebug() << edit->document()->find( "\\x2029" ).position();
  11. qDebug() << edit->document()->find( QRegExp( "\\x2029|\\r\\n|\\r|\\n" ) ).position();
  12. qDebug() << edit->document()->find( QRegularExpression( "\\x2029|\\r\\n|\\r|\\n" ) ).position();
  13. qDebug() << edit->document()->find( QRegularExpression( "\\R" ) ).position();
  14.  
  15. qDebug() << plainedit->document()->find( "\n" ).position();
  16. qDebug() << plainedit->document()->find( "\r\n" ).position();
  17. qDebug() << plainedit->document()->find( "\\x2029" ).position();
  18. qDebug() << plainedit->document()->find( QRegExp( "\\x2029|\\r\\n|\\r|\\n" ) ).position();
  19. qDebug() << plainedit->document()->find( QRegularExpression( "\\x2029|\\r\\n|\\r|\\n" ) ).position();
  20. qDebug() << plainedit->document()->find( QRegularExpression( "\\R" ) ).position();
  21.  
  22. QTextCursor c = plainedit->document()->find( QRegularExpression( "$" ) );
  23. qDebug() << c.hasSelection() << c.selectionStart() << c.selectionEnd();
To copy to clipboard, switch view to plain text mode