Fred
2nd January 2021, 11:02
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.
QTextEdit *edit = new QTextEdit( this );
QPlainTextEdit *plainedit = new QPlainTextEdit( this );
QString Text = "Text\nwith\nnewlines\n\n";
edit->setText( Text );
plainedit->setPlainText( Text );
qDebug() << edit->document()->find( "\n" ).position();
qDebug() << edit->document()->find( "\r\n" ).position();
qDebug() << edit->document()->find( "\\x2029" ).position();
qDebug() << edit->document()->find( QRegExp( "\\x2029|\\r\\n|\\r|\\n" ) ).position();
qDebug() << edit->document()->find( QRegularExpression( "\\x2029|\\r\\n|\\r|\\n" ) ).position();
qDebug() << edit->document()->find( QRegularExpression( "\\R" ) ).position();
qDebug() << plainedit->document()->find( "\n" ).position();
qDebug() << plainedit->document()->find( "\r\n" ).position();
qDebug() << plainedit->document()->find( "\\x2029" ).position();
qDebug() << plainedit->document()->find( QRegExp( "\\x2029|\\r\\n|\\r|\\n" ) ).position();
qDebug() << plainedit->document()->find( QRegularExpression( "\\x2029|\\r\\n|\\r|\\n" ) ).position();
qDebug() << plainedit->document()->find( QRegularExpression( "\\R" ) ).position();
QTextCursor c = plainedit->document()->find( QRegularExpression( "$" ) );
qDebug() << c.hasSelection() << c.selectionStart() << c.selectionEnd();
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.
QTextEdit *edit = new QTextEdit( this );
QPlainTextEdit *plainedit = new QPlainTextEdit( this );
QString Text = "Text\nwith\nnewlines\n\n";
edit->setText( Text );
plainedit->setPlainText( Text );
qDebug() << edit->document()->find( "\n" ).position();
qDebug() << edit->document()->find( "\r\n" ).position();
qDebug() << edit->document()->find( "\\x2029" ).position();
qDebug() << edit->document()->find( QRegExp( "\\x2029|\\r\\n|\\r|\\n" ) ).position();
qDebug() << edit->document()->find( QRegularExpression( "\\x2029|\\r\\n|\\r|\\n" ) ).position();
qDebug() << edit->document()->find( QRegularExpression( "\\R" ) ).position();
qDebug() << plainedit->document()->find( "\n" ).position();
qDebug() << plainedit->document()->find( "\r\n" ).position();
qDebug() << plainedit->document()->find( "\\x2029" ).position();
qDebug() << plainedit->document()->find( QRegExp( "\\x2029|\\r\\n|\\r|\\n" ) ).position();
qDebug() << plainedit->document()->find( QRegularExpression( "\\x2029|\\r\\n|\\r|\\n" ) ).position();
qDebug() << plainedit->document()->find( QRegularExpression( "\\R" ) ).position();
QTextCursor c = plainedit->document()->find( QRegularExpression( "$" ) );
qDebug() << c.hasSelection() << c.selectionStart() << c.selectionEnd();