Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QPlainTextEdit with QSyntaxHighlighter turn highlighting on and off



  • hi there,

    i have a QPlainTextEdit that i am using a QSyntaxHighlighter with. all is working well and syntax is correctly highlighted while editing and on file loading.

    there are 2 things i would like to achieve:

    1. the most important thing is, on file open/load, to have syntax highlighting turned off for the load/open (that is, load as unhighlighted plain text) and then have the highlighting occur after the text is loaded. (the main reason for this is loading a large text that needs highlighting causes the text edit to be blank until the highlighting is finished.)

    2. the second thing is to allow the user to toggle syntax highlighting on and off, that is, when it is turned off the text is displayed unhighlighted, and when it is turned on text is displayed highlighted.

    i've tried many things from the pyside2 and qt5 documentation and from forums and mailing lists, but i cannot get this to work.

    things i've tried include:

    setting the QSyntaxHighlighter document to an empty QTextDocument(), and then back to the QPlainTextEdit.document(). this apparently should work but it does not.

    having a flag in my ui that is set to True or False when highlighting is wanted/unwanted and using the flag in QSyntaxHighlighter().highlightBlock().this does not highlight on text load, and after that only within a block then being edited.

    can anyone point me in the right direction to achieve this in pyside2 code?

    sorry that i've cross-posted this to the pyside2 mailing list, but i just noticed that list doesn't seem to have much traffic.

    thanks for your time for any replies.



  • @SGaist thanks for reply, this was with latest pyside2 installed via pip.

    as it turns out i was sent an interesting reply by personal mail to this question on the mailing list and am currently evaluating that. it seems to work on my initial testing.

    basically this solution involves having 2 syntaxhighlighers, the one that actually highlights, and one that is just a no-op. then you switch the document highlighter between them and run rehihighlight() to turn highlighting on and off.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you show the code you are currently using ?
    What version of PySide2 are you using ?
    How did you install it ?



  • @SGaist thanks for reply, this was with latest pyside2 installed via pip.

    as it turns out i was sent an interesting reply by personal mail to this question on the mailing list and am currently evaluating that. it seems to work on my initial testing.

    basically this solution involves having 2 syntaxhighlighers, the one that actually highlights, and one that is just a no-op. then you switch the document highlighter between them and run rehihighlight() to turn highlighting on and off.


Log in to reply