Results 1 to 5 of 5

Thread: Find newline character in a QTextEdit / QPlainTextEdit

  1. #1
    Join Date
    Apr 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Find newline character in a QTextEdit / QPlainTextEdit

    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 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Find newline character in a QTextEdit / QPlainTextEdit

    QTextEdit and QPlainTextEdit both store their contents as a QTextDocument. When you put text into QTextDocument that contains embedded newline characters, it is probable that these are stripped out and each segment converted into a QTextBlock. So searching for newlines doesn't work because they aren't there.

    You can check this by asking the document for the blockCount(). You could also look at the result of characterAt() where you give it a known index of a newline in your input text.

    You can retrieve something like the original text using QTextDocument::toPlainText() or QTextDocument::toRawText(), but see the comments for those methods regarding changes that could be made from the original input.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Apr 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Find newline character in a QTextEdit / QPlainTextEdit

    Thanks for your answer! I have tried this with
    Qt Code:
    1. for ( int i = 3; i <= 5; ++i ) { qDebug() << plainedit->document()->characterAt( i ); }
    To copy to clipboard, switch view to plain text mode 
    And get as output
    Qt Code:
    1. 't'
    2. '\u2029'
    3. 'w'
    To copy to clipboard, switch view to plain text mode 
    As it looks the newline character is present. But of course it is quite possible that internally it works like you say. So I will have to make a workaround to solve the problem. Thanks for the answer anyway!

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Find newline character in a QTextEdit / QPlainTextEdit

    So I will have to make a workaround to solve the problem.
    What exactly -is- the problem? That is, why are you trying to find the newline characters in the first place? I presume you want to do this with text the user has typed into the editor, because if you are simply setting the text programmatically you already know where the newlines are.

    If the text editor automatically creates a new QTextBlock every time the user enters a carriage return, then the blocks already define the locations of newlines.

    I suspect that characterAt() is working off of the plain text / raw text, not off of the block-structured document. Try retrieving the plain text and/or raw text and step through those strings with the debugger to see if the newlines are there. If they are, then you just need to go one level deeper - plainedit->document()->toPlainText().indexOf( '\n' )
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Apr 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Find newline character in a QTextEdit / QPlainTextEdit

    I am working on a small text editor, search and replace are already implemented and work 100%. Only the search and replace of \n does not work, hence the question.

Similar Threads

  1. find the character width in the console
    By sajis997 in forum Newbie
    Replies: 1
    Last Post: 21st July 2015, 14:43
  2. Replies: 3
    Last Post: 27th July 2014, 11:50
  3. QPlainTextEdit::find() highlighting
    By cic1988 in forum Qt Programming
    Replies: 0
    Last Post: 15th July 2014, 15:21
  4. Replies: 3
    Last Post: 16th January 2011, 10:33
  5. tab character in QTextEdit
    By johnmauer in forum Qt Programming
    Replies: 0
    Last Post: 1st November 2010, 17:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.