Results 1 to 1 of 1

Thread: QPlainTextEdit::find() highlighting

  1. #1
    Join Date
    Jun 2014
    Posts
    20
    Thanks
    2

    Default QPlainTextEdit::find() highlighting

    Hello,

    maybe some of you have worked with the sample code, which finds the matched cases and highlights the results:

    Qt Code:
    1. void MyEditor :: highlight(const QString& txt)
    2. {
    3. QList<QTextEdit::ExtraSelection> extraSelections;
    4.  
    5. if (txt.isEmpty()) {
    6. setExtraSelections(extraSelections);
    7. return;
    8. }
    9.  
    10. QTextDocument::FindFlags flags = ...;
    11. QColor color(Qt::lightGray);
    12.  
    13. while (find(txt, flags)) {
    14. QTextEdit::ExtraSelection extra;
    15. extra.format.setBackground(color);
    16. extra.cursor = textCursor();
    17. extraSelections.append(extra);
    18. }
    19. setExtraSelections(extraSelections);
    20. }
    To copy to clipboard, switch view to plain text mode 

    so the code worked fine. text are found and highlighted from where the cursor stays till document's end.

    however, when the text is very long, e.g 10000 lines. the highlighting behavior costs much time.
    i thought where qplaintextedit had provided a way to update the highlighted text only in the visible page?

    the code i looked into has some seemingly correct alternatives like viewportEvent(), updateRequest() etc.. (a mass of things....)

    then it would, much possibly, go into another issues like:
    1. update and highlight the text when user scrolls to see the new content.
    2. when users during the highlighting changes the text
    ...

    mass...

    ----------------------------------------------- im update line ------------------------------------------------------------------

    worked sometime and came up a solution maybe it would help for those have similar problem like me.

    i havent tested it deeply, a potential bug might be a endless loop in the updateHighlight() when it triggers highlight()



    Qt Code:
    1. void MyEditor :: highlight(const Qtring& txt, QTextDocument::FindFlags flags, int start, int end)
    2. {
    3. if (document()) {
    4. QList<QTextEdit::ExtraSelection> extraSelections;
    5. QColor color(Qt::lightGray);
    6.  
    7. QTextCursor cursor(document());
    8. cursor.setPosition(start);
    9. cursor = document() -> find(txt, cursor, flags);
    10.  
    11. while (! cursor.isNull()) {
    12. if (cursor.position() > end) {
    13. break;
    14. }
    15. QTextEdit::ExtraSelection extra;
    16. extra.format.setBackground(color);
    17. extra.cursor = cursor;
    18. extraSelections.append(extra);
    19.  
    20. cursor = document() -> find(txt, cursor, flags);
    21. }
    22. setExtraSelections(extraSelections);
    23. }
    24. }
    25.  
    26.  
    27. void MyEditor :: updateHighlight(const QRect& rect, int dy)
    28. {
    29. if (dy != 0) {
    30. if (isHighlightEnabled) {
    31.  
    32. //------------------------------------------------------------------------------
    33. // update only the visible part
    34. //------------------------------------------------------------------------------
    35.  
    36. QTextCursor cursor = cursorForPosition(QPoint(0, 0));
    37. QPoint pageY = viewport() ? QPoint(viewport() -> width() - 1, viewport() -> height() - 1) : QPoint(0, 0);
    38. highlight(txt, findFlags, cursor.position(), cursorForPosition(pageY).position());
    39. }
    40. }
    41. }
    42.  
    43. MyEditor :: MyEditor(QObject* p)
    44. {
    45. connect(this, SIGNAL(updateRequest(const QRect&, int)), SLOT(updateHighlight(const QRect&, int)));
    46. }
    To copy to clipboard, switch view to plain text mode 

    instead of using QPlainTextEdit::find() which move the cursor eachtime when he finds a result, QTextDocument::find() doesnt do that.
    Last edited by cic1988; 15th July 2014 at 16:48. Reason: updated contents

Similar Threads

  1. Replies: 1
    Last Post: 28th May 2012, 15:06
  2. Problem of highlighting text in QPlainTextEdit
    By topfortune in forum Qt Programming
    Replies: 5
    Last Post: 19th April 2012, 02:51
  3. QwtSymbol highlighting?
    By shud in forum Qwt
    Replies: 1
    Last Post: 2nd October 2009, 21:42
  4. highlighting a QPushButton
    By qtUser500 in forum Qt Programming
    Replies: 6
    Last Post: 14th July 2009, 20:21
  5. Highlighting QTreeWidgetItem
    By rajesh210 in forum Qt Programming
    Replies: 1
    Last Post: 23rd July 2008, 11:51

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.