Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to avoid QSyntaxHighlighter::highlightBlock blocking the UI thread
Forum Update on Monday, May 27th 2025

How to avoid QSyntaxHighlighter::highlightBlock blocking the UI thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 619 Views
  • 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.
  • K Offline
    K Offline
    Krantz
    wrote on 21 Nov 2018, 03:23 last edited by
    #1

    I currently use a subclass of QSyntaxHighlighter to highlight my document. In the highlight example, everything was okay, because the tokenization is implemented using regular expressions, and can be done in a local range.

    But I prefer to have a parser which provides more semantic information, so I use libclang as a backend. The parser of libclang literally can take a LONG time to gather all the information. During this process, the UI thread is blocked, for highlightBlock seems to be called synchronized, i.e. the UI thread waits for the function to finish.

    So I wonder whether there is a way to make this process asynchronous so that the UI is no longer blocked during the highlight process.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 21 Nov 2018, 04:17 last edited by
      #2

      You need to avoid doing the heavy task in UI Thread. Move your lib clang functionality concurrent task. You use QtConcurrent or use QThread to make the task concurrent.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Krantz
        wrote on 21 Nov 2018, 09:00 last edited by
        #3

        I think I understand that I should move the parsing task out of the function, but the problem really is how.

        The main difficulty is: the clang API needs a source position to locate the token information, so I could only update the parse tree AFTER the text changes. But as I experimented, the signal textChanged or contentsChanged are both emitted after the slot highlightBlock is called (highlightBlock is called immediately after the text changes), so I had no opportunity to parse the file in advance.

        After highlightBlock returns, I found no approach to highlight the text, even if I have already known how it should be highlighted.

        So is there any way that I can somehow delay the highlight process, and do it when the application is fully prepared?

        1 Reply Last reply
        0

        3/3

        21 Nov 2018, 09:00

        • Login

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