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. TextChanged triggered in QPlainTextEdit even if signals blocked when using QSyntaxHighlighter
Forum Updated to NodeBB v4.3 + New Features

TextChanged triggered in QPlainTextEdit even if signals blocked when using QSyntaxHighlighter

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.9k 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.
  • F Offline
    F Offline
    fscibilia
    wrote on last edited by
    #1

    Hi all,

    I'm using QPlainTextEdit with QSyntaxHighlighter with signals blocked in QPlainTextEdit. I'm using a test code like this (within the constructor of a class derived by QMdiSubWindow):

    @
    editor_ = new QPlainTextEdit("Hello", this);
    editor_->blockSignals(true);
    connect(editor_, SIGNAL(textChanged()), SLOT(onTextChanged()));
    editor_->textCursor().insertText(text);
    ScriptSyntaxHighlighter* highlighter = new ScriptSyntaxHighlighter(editor_->document());
    setWidget(editor_);
    editor_->blockSignals(false);
    @

    Strangely, while the instruction
    @
    textCursor().insertText()
    @

    does not trigger textChanged (as expected due to signals area blocked), the instruction
    @
    new ScriptSyntaxHighlighter(editor_->document())
    @

    triggers the signal (even if it invoked while signals are blocked). However, I noted that the in the first case (insertText), the signal is emitted in DirectConnection with slots invoked immediately, while the textChanged() triggered by QSyntaxHighlighter is emitted in QueuedConnection at the end of the GUI event loop (after the blockSignals set to false).

    Even if not a bug, I think it is an unwanted behaviour.

    Any suggestion? May be is a bug? Any workaround? At the moment, I'm simply ignoring the first signal emitted (not elegand but anyway working).

    Thanks
    Fabio

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

      Hi,

      What happens if you first set the highlighter and then the text ?

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

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fscibilia
        wrote on last edited by
        #3

        Hi SGaist,

        I tried your suggestion and it works. The new snippet is this one:

        @
        editor_ = new QPlainTextEdit("Hello", this);
        editor_->blockSignals(true);
        ScriptSyntaxHighlighter* highlighter = new ScriptSyntaxHighlighter(editor_->document());
        connect(editor_, SIGNAL(textChanged()), SLOT(onTextChanged()));
        editor_->textCursor().insertText(text);
        setWidget(editor_);
        editor_->blockSignals(false);
        @

        With the above code no signals are emitted. It seems to me that setting the syntax highlighter after the insertion of a text produces the emission of a new textChanged() signals in the next GUI even loop (some cascade of signals produced in QueuedConnection). This is a bit misleading for the programmer :(.

        In any case my problem is solved, thanks for your help

        Fabio

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

          Was textChanged triggered only once or repeatedly with your original code ?

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

          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