Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QPlainTextEdit with QSyntaxHighlighter turn highlighting on and off
Forum Updated to NodeBB v4.3 + New Features

QPlainTextEdit with QSyntaxHighlighter turn highlighting on and off

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 892 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elguavas
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS SGaist

      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 ?

      E Offline
      E Offline
      elguavas
      wrote on last edited by
      #3

      @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.

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        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 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        E 1 Reply Last reply
        0
        • SGaistS SGaist

          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 ?

          E Offline
          E Offline
          elguavas
          wrote on last edited by
          #3

          @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.

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved