Results 1 to 3 of 3

Thread: Forcing a QTextEdit to highlight

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Forcing a QTextEdit to highlight

    Hi,

    I wrote a small RegExp tool and I have the following problem:
    The tool has two text fields - one where the user can type the regular expression (QPlainTextEdit), and another which contains a test text and on which the current regular expression is being executed (QTextEdit).
    As the user is typing the expression, I want to highlight the matching text in the test text QTextEdit.
    To achieve this I used QSyntaxHighlighter.
    It all works nicely, except, that the QSyntaxHighlighter::highlightBlock() is called by the document when it was changed.
    Since I am not changing the test text but the RegExp, the highlighting doesn't get triggered unless I change the test text.

    My question is:
    Does any one know if there is a way to force QTextEdit to emit textChanged() or any similar notification that will trigger the highlighting without subclassing QTextEdit?
    Just out of curiosity.

    Thanks.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  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: Forcing a QTextEdit to highlight

    There is another approach you can take. Make use of QTextEdit::setExtraSelections(). It should be much simpler to use than the syntax highlighter.
    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. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Forcing a QTextEdit to highlight

    Thanks for the tip Witek.
    I had a look at it, and implemented it too (just to see what would be the difference), and I must say I don't find it any easier, nor in any way better than using the syntax highlighter, on so many levels.
    The QSyntaxHighlighter is by far a cleaner design (specially from encapsulation and open-close perspectives), and more efficient - at least in the sense of the code it forces you to implement.
    I'll try sub classing QTextEdit and allow a spontaneous (extern) refresh of the highlighter, and see if it works, if not, I'll fall back to the setExtraSelections() solution.

    Cheers.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. QTextEdit contents changed highlight the color
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 3
    Last Post: 8th June 2011, 17:31
  2. forcing dialog focus?
    By jajdoo in forum Newbie
    Replies: 1
    Last Post: 3rd September 2010, 20:17
  3. highlight in QTextEdit
    By ubuntudevelop78 in forum Qt Programming
    Replies: 1
    Last Post: 27th November 2009, 00:22
  4. Forcing a signal?
    By dbrmik in forum Newbie
    Replies: 1
    Last Post: 6th January 2009, 11:10
  5. Forcing two widgets to the same size
    By dsandber in forum Qt Tools
    Replies: 3
    Last Post: 7th October 2007, 10:11

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.