Best practice for QT update of signal / slot values

What do you consider best practice for developing signal / slot interactions to update element values ​​in a class?

For example, consider a member variable that is presented in the user interface. The user changes the value in the user interface. There is a signal / slot relationship for automatically updating a member variable through the member variable updating function.

We also want the changes in the member variable to automatically update in the user interface, so the signal / slot ratio happens differently. When updating a member variable through the update function, the signal triggers the user interface that needs to be updated.

How do you prevent their spread? Is it as simple as checking a new value for the current value when calling the update function of a member variable and only sending a signal to update the user interface, if there is a difference?

Or ... is there a more elegant way to do this?

+4
source share
1 answer

How do you prevent their spread? Is it as simple as checking a new value for the current value when calling the update function of a member variable and only sending a signal to update the user interface, if there is a difference?

Yes.

Pragmatically speaking, this allows you to connect, say, QDial, QSpinBox and QSlider, and make them stay in sync without requiring you to add extra magic to prevent endless loops.

Semantically speaking, have you noticed that a typical signal when changing a value is called the value Modified ?

void myClass::setValue(int value) { if (m_value != value) { m_value = value; emit valueChanged(value); // YES, THE VALUE *DID* CHANGE! } } 

This means that the signal should not be emitted if there is no change in the value, which happens if you try to set the value to the current one, either by setting it directly or through the signal / slot invokation.

+6
source

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


All Articles