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. QSyntaxHighlighter rehighlight() ignores changes triggered from another thread
Forum Updated to NodeBB v4.3 + New Features

QSyntaxHighlighter rehighlight() ignores changes triggered from another thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 5 Posters 2.0k Views 3 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.
  • R Offline
    R Offline
    ribtoks
    wrote on last edited by ribtoks
    #1

    I have a spellchecking thread which fires spellcheck() signals from time to time which are connected to my highlighter's rehighlight() method. The latter sets the whole block to have red foreground.

    This used to work in Qt 5.6.2 and ceased to work in newer versions. I hopelessly waited for it to get fixed in Qt 5.9.5, but it still does not work (neither in Windows 10, nor in OS X).

    void SpellCheckErrorsHighlighter::highlightBlock(const QString &text) {
        if (!m_Worker->isOK()) { return; }
    
        this->setFormat(0, text.length(), QColor(0xff, 0, 0));
    }
    
    // ------
    
    void MainModel::startChecking() {
        m_Worker = new SpellCheckWorker();
        QThread *thread = new QThread();
        m_Worker->moveToThread(thread);
    
        QObject::connect(thread, &QThread::started, m_Worker, &SpellCheckWorker::process);
        thread->start();
    }
    
    void MainModel::initNameHighlighting(QQuickTextDocument *document) {
        SpellCheckErrorsHighlighter *highlighter = new SpellCheckErrorsHighlighter(m_Worker, document->textDocument());
        QObject::connect(m_Worker, &SpellCheckWorker::spellcheck,
                         highlighter, &SpellCheckErrorsHighlighter::rehighlight);
    }
    
    // ------
    
    void SpellCheckWorker::process() {
        while (1) {
            m_Counter++;
            m_IsOK = m_Counter % 7 == 0;
            QThread::sleep(1);
            emit spellcheck();
       }
    }
    
    // ------
    
    TextEdit {
        id: titleTextInput
        width: paintedWidth > titleFlick.width ? paintedWidth : titleFlick.width
        height: titleFlick.height
        text: mainModel.name
        focus: true
        onTextChanged: mainModel.name = text
    
        Component.onCompleted: mainModel.initNameHighlighting(titleTextInput.textDocument)
        onCursorRectangleChanged: titleFlick.ensureVisible(cursorRectangle)
    }
    
    // -----
    
    int main(int argc, char *argv[]) {
        QGuiApplication app(argc, argv);
    
        MainModel mainModel;
        mainModel.startChecking();
    
        QQmlApplicationEngine engine;
    
        QQmlContext *rootContext = engine.rootContext();
        rootContext->setContextProperty("mainModel", &mainModel);
    
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        return app.exec();
    }
    

    Small example which reproduces the problem could be obtained from here https://bitbucket.org/ribtoks/qt-highlighting-issue (in order to repro, type something in the input. rehighlight() will be triggered each 7 seconds from background thread)

    Is there any way to get it working with workarounds? I need to keep spellchecking logic in the background thread so it's not possible to move it to main thread.

    aha_1980A 1 Reply Last reply
    0
    • R ribtoks

      I have a spellchecking thread which fires spellcheck() signals from time to time which are connected to my highlighter's rehighlight() method. The latter sets the whole block to have red foreground.

      This used to work in Qt 5.6.2 and ceased to work in newer versions. I hopelessly waited for it to get fixed in Qt 5.9.5, but it still does not work (neither in Windows 10, nor in OS X).

      void SpellCheckErrorsHighlighter::highlightBlock(const QString &text) {
          if (!m_Worker->isOK()) { return; }
      
          this->setFormat(0, text.length(), QColor(0xff, 0, 0));
      }
      
      // ------
      
      void MainModel::startChecking() {
          m_Worker = new SpellCheckWorker();
          QThread *thread = new QThread();
          m_Worker->moveToThread(thread);
      
          QObject::connect(thread, &QThread::started, m_Worker, &SpellCheckWorker::process);
          thread->start();
      }
      
      void MainModel::initNameHighlighting(QQuickTextDocument *document) {
          SpellCheckErrorsHighlighter *highlighter = new SpellCheckErrorsHighlighter(m_Worker, document->textDocument());
          QObject::connect(m_Worker, &SpellCheckWorker::spellcheck,
                           highlighter, &SpellCheckErrorsHighlighter::rehighlight);
      }
      
      // ------
      
      void SpellCheckWorker::process() {
          while (1) {
              m_Counter++;
              m_IsOK = m_Counter % 7 == 0;
              QThread::sleep(1);
              emit spellcheck();
         }
      }
      
      // ------
      
      TextEdit {
          id: titleTextInput
          width: paintedWidth > titleFlick.width ? paintedWidth : titleFlick.width
          height: titleFlick.height
          text: mainModel.name
          focus: true
          onTextChanged: mainModel.name = text
      
          Component.onCompleted: mainModel.initNameHighlighting(titleTextInput.textDocument)
          onCursorRectangleChanged: titleFlick.ensureVisible(cursorRectangle)
      }
      
      // -----
      
      int main(int argc, char *argv[]) {
          QGuiApplication app(argc, argv);
      
          MainModel mainModel;
          mainModel.startChecking();
      
          QQmlApplicationEngine engine;
      
          QQmlContext *rootContext = engine.rootContext();
          rootContext->setContextProperty("mainModel", &mainModel);
      
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      
          return app.exec();
      }
      

      Small example which reproduces the problem could be obtained from here https://bitbucket.org/ribtoks/qt-highlighting-issue (in order to repro, type something in the input. rehighlight() will be triggered each 7 seconds from background thread)

      Is there any way to get it working with workarounds? I need to keep spellchecking logic in the background thread so it's not possible to move it to main thread.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ribtoks said in QSyntaxHighlighter rehighlight() ignores changes triggered from another thread:

      I hopelessly waited for it to get fixed in Qt 5.9.5

      Have you ever created a bug report on bugreports.qt.io ?

      Qt has to stay free or it will die.

      R 1 Reply Last reply
      1
      • aha_1980A aha_1980

        @ribtoks said in QSyntaxHighlighter rehighlight() ignores changes triggered from another thread:

        I hopelessly waited for it to get fixed in Qt 5.9.5

        Have you ever created a bug report on bugreports.qt.io ?

        R Offline
        R Offline
        ribtoks
        wrote on last edited by
        #3

        @aha_1980 said in QSyntaxHighlighter rehighlight() ignores changes triggered from another thread:

        @ribtoks said in QSyntaxHighlighter rehighlight() ignores changes triggered from another thread:

        I hopelessly waited for it to get fixed in Qt 5.9.5

        Have you ever created a bug report on bugreports.qt.io ?

        Of course I did. It's never fixed P2. Qt people never care about such bugs, they only care about fixing enterprise issues..
        https://bugreports.qt.io/browse/QTBUG-65248

        aha_1980A 1 Reply Last reply
        0
        • R ribtoks

          @aha_1980 said in QSyntaxHighlighter rehighlight() ignores changes triggered from another thread:

          @ribtoks said in QSyntaxHighlighter rehighlight() ignores changes triggered from another thread:

          I hopelessly waited for it to get fixed in Qt 5.9.5

          Have you ever created a bug report on bugreports.qt.io ?

          Of course I did. It's never fixed P2. Qt people never care about such bugs, they only care about fixing enterprise issues..
          https://bugreports.qt.io/browse/QTBUG-65248

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @ribtoks

          The report was one week before christmas, so we effectively have three month since the report. I would not call that never.

          Furthermore, if you have a commercial license, let the commercial support add the corresponding tag to the report. Also Qt developers need to pay their flat and get something to eat and drink.

          If you don't have, there is more you can do. If 5.6.2 is the last working version, you can try to find the first broken version. Is it 5.7, 5.8, or 5.9? Or was it in a patch release?

          You can even go a step further and do git bisect to find the first broken commit. That's all a lot of work that keeps the developer from developing (and fixing bugs).

          And last but not least, you are the only watcher of this bug, so I guess it's just a corner case you have hit. There are other bugs that effect a lot of people and that are therefore with higher priority.

          Thanks.

          Qt has to stay free or it will die.

          R 1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            Ran your bug report sample in 5.10.1
            alt text

            Is that the expected non working output ?

            R 1 Reply Last reply
            0
            • aha_1980A aha_1980

              @ribtoks

              The report was one week before christmas, so we effectively have three month since the report. I would not call that never.

              Furthermore, if you have a commercial license, let the commercial support add the corresponding tag to the report. Also Qt developers need to pay their flat and get something to eat and drink.

              If you don't have, there is more you can do. If 5.6.2 is the last working version, you can try to find the first broken version. Is it 5.7, 5.8, or 5.9? Or was it in a patch release?

              You can even go a step further and do git bisect to find the first broken commit. That's all a lot of work that keeps the developer from developing (and fixing bugs).

              And last but not least, you are the only watcher of this bug, so I guess it's just a corner case you have hit. There are other bugs that effect a lot of people and that are therefore with higher priority.

              Thanks.

              R Offline
              R Offline
              ribtoks
              wrote on last edited by
              #6

              @aha_1980 said in QSyntaxHighlighter rehighlight() ignores changes triggered from another thread:

              @ribtoks

              The report was one week before christmas, so we effectively have three month since the report. I would not call that never.

              Furthermore, if you have a commercial license, let the commercial support add the corresponding tag to the report. Also Qt developers need to pay their flat and get something to eat and drink.

              If you don't have, there is more you can do. If 5.6.2 is the last working version, you can try to find the first broken version. Is it 5.7, 5.8, or 5.9? Or was it in a patch release?

              You can even go a step further and do git bisect to find the first broken commit. That's all a lot of work that keeps the developer from developing (and fixing bugs).

              And last but not least, you are the only watcher of this bug, so I guess it's just a corner case you have hit. There are other bugs that effect a lot of people and that are therefore with higher priority.

              Thanks.

              The bug first appeared in 5.6.3 and later. Obviously I don't have commercial license otherwise I wouldn't complain on forums but called support directly.

              Are you kidding regarding git bisect? I cannot waste days rebuilding Qt all the time with different patches and then running my test against it by hand. It cannot be detected automatically, unfortunately.

              Yes, I'm the only watcher, but I wouldn't call it such an edge case - to desire Qt to behave accordingly to specification (when you do setFormat() for the format to change).

              I believe this should be very easy to fix for a person who is familiar with the codebase. Probably just description of the problem would be enough for such person, but there's also a working sample.

              aha_1980A 1 Reply Last reply
              0
              • mrjjM mrjj

                Hi
                Ran your bug report sample in 5.10.1
                alt text

                Is that the expected non working output ?

                R Offline
                R Offline
                ribtoks
                wrote on last edited by
                #7

                @mrjj said in QSyntaxHighlighter rehighlight() ignores changes triggered from another thread:

                Hi
                Ran your bug report sample in 5.10.1
                alt text

                Is that the expected non working output ?

                Hi. I will check it in 5.10.1 tomorrow, thanks!
                Sometimes if you manually put cursor into the TextEdit it gets updated and turns red, but it's not always like that.
                If you can repro it consistently then maybe it's working in 5.10.1 (I wanted to LTS), I'll try tomorrow.

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  I tested it and it looks like it works as expected - every 8 seconds the text is marked completely red when I type something.
                  But the way you check for isOK() is for sure not what you want. You want an atomic here. If you don't trust me, see for example here https://stackoverflow.com/questions/4557979/when-to-use-volatile-with-multi-threading

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  R 1 Reply Last reply
                  3
                  • Christian EhrlicherC Christian Ehrlicher

                    I tested it and it looks like it works as expected - every 8 seconds the text is marked completely red when I type something.
                    But the way you check for isOK() is for sure not what you want. You want an atomic here. If you don't trust me, see for example here https://stackoverflow.com/questions/4557979/when-to-use-volatile-with-multi-threading

                    R Offline
                    R Offline
                    ribtoks
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher said in QSyntaxHighlighter rehighlight() ignores changes triggered from another thread:

                    I tested it and it looks like it works as expected - every 8 seconds the text is marked completely red when I type something.
                    But the way you check for isOK() is for sure not what you want. You want an atomic here. If you don't trust me, see for example here https://stackoverflow.com/questions/4557979/when-to-use-volatile-with-multi-threading

                    Hi @Christian-Ehrlicher
                    Thanks for trying it out. Could you please let me know it works as expected using Qt of which version?
                    Yeah, I know about atomics, this was just super dirty hack not to rehighlight each second. My real code does not have this garbage.

                    1 Reply Last reply
                    0
                    • R ribtoks

                      @aha_1980 said in QSyntaxHighlighter rehighlight() ignores changes triggered from another thread:

                      @ribtoks

                      The report was one week before christmas, so we effectively have three month since the report. I would not call that never.

                      Furthermore, if you have a commercial license, let the commercial support add the corresponding tag to the report. Also Qt developers need to pay their flat and get something to eat and drink.

                      If you don't have, there is more you can do. If 5.6.2 is the last working version, you can try to find the first broken version. Is it 5.7, 5.8, or 5.9? Or was it in a patch release?

                      You can even go a step further and do git bisect to find the first broken commit. That's all a lot of work that keeps the developer from developing (and fixing bugs).

                      And last but not least, you are the only watcher of this bug, so I guess it's just a corner case you have hit. There are other bugs that effect a lot of people and that are therefore with higher priority.

                      Thanks.

                      The bug first appeared in 5.6.3 and later. Obviously I don't have commercial license otherwise I wouldn't complain on forums but called support directly.

                      Are you kidding regarding git bisect? I cannot waste days rebuilding Qt all the time with different patches and then running my test against it by hand. It cannot be detected automatically, unfortunately.

                      Yes, I'm the only watcher, but I wouldn't call it such an edge case - to desire Qt to behave accordingly to specification (when you do setFormat() for the format to change).

                      I believe this should be very easy to fix for a person who is familiar with the codebase. Probably just description of the problem would be enough for such person, but there's also a working sample.

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @ribtoks said in QSyntaxHighlighter rehighlight() ignores changes triggered from another thread:

                      The bug first appeared in 5.6.3 and later.

                      So it is a regression from 5.6.2 to 5.6.3? I wonder why this important information is not stated in the bug report.

                      Are you kidding regarding git bisect? I cannot waste days rebuilding Qt all the time with different patches and then running my test against it by hand. It cannot be detected automatically, unfortunately.

                      And therefore you expect others to search for the problem?

                      Qt has to stay free or it will die.

                      R 1 Reply Last reply
                      0
                      • aha_1980A aha_1980

                        @ribtoks said in QSyntaxHighlighter rehighlight() ignores changes triggered from another thread:

                        The bug first appeared in 5.6.3 and later.

                        So it is a regression from 5.6.2 to 5.6.3? I wonder why this important information is not stated in the bug report.

                        Are you kidding regarding git bisect? I cannot waste days rebuilding Qt all the time with different patches and then running my test against it by hand. It cannot be detected automatically, unfortunately.

                        And therefore you expect others to search for the problem?

                        R Offline
                        R Offline
                        ribtoks
                        wrote on last edited by
                        #11

                        @aha_1980 I expect "others" who know this particular code to look at the code, not to run git bisect and test every other patch. I believe there's a person writing most of the QTextDocument and QTextDocumentPrivate and who knows how it works. I don't know how it works and it will probably take me quite some time, not mentioning time to setup build environment for Qt.

                        Qt 5.6.x was LTS where people continuously fix stuff, not break stuff which used to work before. And it's just ridiculous somebody assigned P2 "Critical" to the issue, but nobody cares to fix. I would understand if it was P6 and nobody cared.

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          It works with 5.10.0 / linux for me.
                          btw: I don't see any significant changes in qsyntaxhighlighter.cpp for the last years

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          R 1 Reply Last reply
                          2
                          • Christian EhrlicherC Christian Ehrlicher

                            It works with 5.10.0 / linux for me.
                            btw: I don't see any significant changes in qsyntaxhighlighter.cpp for the last years

                            R Offline
                            R Offline
                            ribtoks
                            wrote on last edited by
                            #13

                            @Christian-Ehrlicher said in QSyntaxHighlighter rehighlight() ignores changes triggered from another thread:

                            It works with 5.10.0 / linux for me.
                            btw: I don't see any significant changes in qsyntaxhighlighter.cpp for the last years

                            The change could be in QTextDocument or TextEdit (the end result is that TextEdit does not update the highlighting). I never said the bug is inside QSyntaxHighlighter. Thanks for checking it btw!

                            For me it does not work on macOS and Windows.

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

                              Hi,

                              Got text hightlighted on macOS from time to time on macOS with 5.10 and 5.11 self-built.

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

                              R 1 Reply Last reply
                              0
                              • SGaistS SGaist

                                Hi,

                                Got text hightlighted on macOS from time to time on macOS with 5.10 and 5.11 self-built.

                                R Offline
                                R Offline
                                ribtoks
                                wrote on last edited by
                                #15

                                @SGaist said in QSyntaxHighlighter rehighlight() ignores changes triggered from another thread:

                                Hi,

                                from time to time on macOS

                                Hello. Yes, that's the problem, "from time to time". Thanks for trying it out btw!

                                1 Reply Last reply
                                0

                                • Login

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