Results 1 to 2 of 2

Thread: QPlainTextEdit highlight word

  1. #1
    Join Date
    Jan 2011
    Posts
    23
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default QPlainTextEdit highlight word

    Hi,

    In my texteditor application I'd like to highlight all occurrence of a word under cursor. The problem is that it works for all lines except highlighted line.
    Qt Code:
    1. void CodeEditor::test()
    2. {
    3. highlightCurrentLine(); //function based on CodeEditor example
    4. highlightCurrentWord("bar");
    5. }
    6.  
    7. void CodeEditor::highlightCurrentWord(QString curWord)
    8. {
    9. fmt.setBackground(Qt::red);
    10. fmt.setForeground(Qt::black);
    11. //...
    12. QTextCursor cursor = QTextCursor(block);
    13. cursor.select(QTextCursor::WordUnderCursor);
    14. //...
    15. cursor.setPosition(begin, QTextCursor::MoveAnchor);
    16. cursor.setPosition(end, QTextCursor::KeepAnchor);
    17. cursor.mergeCharFormat(fmt); //I did try also cursor.setCharFormat(fmt);
    18. //...
    To copy to clipboard, switch view to plain text mode 

    So for example if I have three lines

    foo xyz bar

    bar edddd

    abc bar

    let's say line 1 is cursor current line and therefore is highlighted. I want to highlight 'bar' word it will be highlighted in lines 2, 3 but not in line 1 cause of line highlight present.

    Anyone know how to solve this issue ? somehow merge formating ?

    Thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPlainTextEdit highlight word

    Qt Code:
    1. void CodeEditor::highlightCurrentLine()
    2. {
    3. QList<QTextEdit::ExtraSelection> extraSelections;
    4.  
    5. if (!isReadOnly()) {
    6. QTextEdit::ExtraSelection selection;
    7.  
    8. QColor lineColor = QColor(Qt::yellow).lighter(160);
    9.  
    10. selection.format.setBackground(lineColor);
    11. selection.format.setProperty(QTextFormat::FullWidthSelection, true);
    12. selection.cursor = textCursor();
    13. selection.cursor.clearSelection();
    14. extraSelections.append(selection);
    15. }
    16.  
    17. // BEGIN: added
    18. QTextCursor cursor = textCursor();
    19. cursor.select(QTextCursor::WordUnderCursor);
    20. QTextEdit::ExtraSelection currentWord;
    21. QColor redColor = Qt::red;
    22. currentWord.format.setBackground(redColor);
    23. currentWord.cursor = cursor;
    24. extraSelections.append(currentWord);
    25. // END: added
    26.  
    27. setExtraSelections(extraSelections);
    28. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    ArkKup (4th March 2013)

Similar Threads

  1. Highlight of an ListView
    By Gihu in forum Qt Quick
    Replies: 1
    Last Post: 2nd July 2012, 17:13
  2. QPlainTextEdit highlight selected line
    By ArkKup in forum Qt Programming
    Replies: 0
    Last Post: 2nd November 2011, 09:45
  3. how to get the word when i highlight a word with mouse?
    By saleh.hi.62 in forum Qt Programming
    Replies: 0
    Last Post: 4th July 2010, 08:24
  4. highlight problem
    By supriyajn in forum Newbie
    Replies: 1
    Last Post: 20th May 2010, 19:00
  5. Highlight row in treeview
    By supergillis in forum Qt Programming
    Replies: 6
    Last Post: 20th November 2008, 08:49

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.