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. Use of QSpinBox::blockSignals. How to avoid infinite loop with valueChanged.

Use of QSpinBox::blockSignals. How to avoid infinite loop with valueChanged.

Scheduled Pinned Locked Moved Solved General and Desktop
qspinboxblocksignalemit signalinfinite loop
4 Posts 2 Posters 3.1k 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.
  • P Offline
    P Offline
    Petross404_Petros S
    wrote on 2 Mar 2018, 20:05 last edited by Petross404_Petros S
    #1

    I leave this in case someone finds this useful.

    Let's say I have a spinBox which is able to set directly an image displayed on a QLabel with valueChanged* signal. The same spinBox is updated ( -> it emits valueChanged again ) every time the image is changed from a pushButton with ::setValue(int)** member function.

    • One way to alter this spinBox, is to interact directly it's arrows to control the image through image_update
    //In the ctor of QtDice
    connect(m_ui->spinBox, QOverload<int>::of(&QSpinBox::valueChanged),
        this, static_cast<void (QtDice::*)(int)>(&QtDice::reload));
    //reload calls image_update
    
    • In here the spinBox is just updated with image_number; no direct user interaction
    //In a member function of QtDice ( void image_update(int) )
    m_ui->spinBox->setValue(image_number);
    //This triggers valueChanged signal leading valueChanged to call this again...
    

    As I noticed each time the spinBox was altered, the image_update(int) was called and each time image_update(int) was called the spinBox was altered leading the program in an endless loop. So I had to found a way to instruct the spinBox to emit valueChanged only if direct user interaction with it took place.

    But how do you instruct a spinBox to emit (kind of) conditionally ? As Chernobyl of StackOverflow pointed out, that's the way to go :

    ....
    m_ui->spinBox->blockSignals(true); //Shush, block your signals
    m_ui->spinBox->setValue(image_number);
    m_ui->spinBox->blockSignals(false); //OK, emit from now on
    ...
    

    That's all, I hope this will be useful to someone one day. Of course other more experienced users may add their their solutions.

    1 Reply Last reply
    1
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Mar 2018, 21:11 last edited by
      #2

      Hi,

      To avoid signals storms you usually have something like this workflow:

      void setMyProperty(int value)
      {
          if (value == _myPropertyValue) {
              return;
          }
          _myPropertyValue = value;
          emit myPropertyChanged(value);
      }
      

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

      P 1 Reply Last reply 3 Mar 2018, 07:36
      1
      • S SGaist
        2 Mar 2018, 21:11

        Hi,

        To avoid signals storms you usually have something like this workflow:

        void setMyProperty(int value)
        {
            if (value == _myPropertyValue) {
                return;
            }
            _myPropertyValue = value;
            emit myPropertyChanged(value);
        }
        
        P Offline
        P Offline
        Petross404_Petros S
        wrote on 3 Mar 2018, 07:36 last edited by
        #3

        @SGaist QSpinBox emits automatically, so why should I do it again?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 3 Mar 2018, 22:13 last edited by
          #4

          Doing so ensure the proper behaviour. If you connect anything that doesn't do it, you'll still avoid the signal storm.

          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
          2

          4/4

          3 Mar 2018, 22:13

          • Login

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