pyqt5 QPlainTextEdit with QSyntaxHighlighter turn highlighting on and off

Elguavas elguavas at
Mon Apr 27 05:35:12 BST 2020

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 cause 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 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 pyqt5 code?

thanks for your time for any replies.

More information about the PyQt mailing list