How does Qt handle an infinitely limited event loop?

I have QSlider and QSpinBox , and I would like their values ​​to always be equal, so I connected the valueChanged(int)signal slider to the spinboard ' setValue(int), and vice versa: (of course, I also set the min and max equal values)

connect(delay_slider, SIGNAL(valueChanged(int)),
        delay_spin, SLOT(setValue(int)));
connect(delay_spin, SIGNAL(valueChanged(int)),
        delay_slider, SLOT(setValue(int)));

I tested and it works (at least on my Ubuntu 12.04 LTS x86_64, g ++ 4.6.3, Qt 4.8.1).

Now, I think that when I emit one of the signals, it calls the other, which will trigger the first, which will call the other, etc. How does Qt handle this? Is there a document describing the mechanisms used?

Review: I called it an "event loop" because it has nothing to do with Qt Event Loop

+4
source share
1 answer

This is just an example from the Signals and Slots Documentation :

 Counter a, b;
 QObject::connect(&a, SIGNAL(valueChanged(int)),
                  &b, SLOT(setValue(int)));

 a.setValue(12);     // a.value() == 12, b.value() == 12
 b.setValue(48);     // a.value() == 12, b.value() == 48

Please note that the function setValue()sets the value and emits a signal only if value != m_value. This prevents endless cycling in the case of cyclic connections (for example, if you b.valueChanged()were connected to a.setValue()).

Thus, in this case, it is a safety feature setValue. This is also described in the function setValue:

setValue()will highlight valueChanged()if the new value is different from the old .

, , , , .

+6

Source: https://habr.com/ru/post/1531876/


All Articles