Cascades and signals / slots

I'm spinning about it. I just can’t wrap my head around signals and slots.

Just find some mechanism that can automatically update my interface when a signal occurs in my C ++.

Example:

I have two labels in Qml that have text: _app.method, which returns a value.

I have a button in which onClicked launches the Q_INVOKABLE method. This method generates a signal when it is executed, for example, it extracts geocoordinates and updates the values ​​indicated above in the text: charges are assigned.

I want SOMETHING to update the text: assignments after changing these values.

I just need these signals / slots. The only examples in the documentation seem to suggest ONLY QML or C ++, but not a combination of both. The sample code has examples, but is not specifically explained in the documentation.

If you had a simple description, I’m sure I can adapt to it. For example, 1: define this in QML, 2: define this in the hpp file, 3: define them in the cpp file.

I tried using QObject setPropery ("text", "value"), but this application fails when trying.

Tell me if I'm wrong ...

1) in QML:

Button { id: aButton text: _app.value onClicked: { _app.valueChanged.connect(aButton.onValueChanged); _app.value = _app.value + 1; } function onValueChanged (val) { aButton.text = "New value: " + val; } } 

2) in HPP:

  Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged) public: int value(); void setValue(int i); signals: void valueChanged(int); private: int m_iValue; 

3) in CPP:

 int class::value() { return m_iValue; } void class::setValue(int i) { // name is same as HPP WRITE Q_PROPERTY statement m_iValue = i; emit valueChanged(m_iValue); } 

So what happens is that in QML the onClick method CONNECTs the signal using the QML function; which means that now we are listening to a change in value, and when this happens, this function will be called. Then we change the value ... since Q_PROPERTY sets the value of the function record called setValue, setValue is called with the new value; internally, m_iValue changes, and an emission occurs that tells the one who is listening to valueChanged that there is a new value.

Hey my QML is listening! (via _app.valueChanged.connect script). So, the QML (Button) object that listened to it has an onValueChanged function, with a new value (due to emit valueChanged (m_iValue).

Please tell me, I understand this?!?!

+6
source share
1 answer

If you use the Q_PROPERTY macro, there is no need to associate the onValueChanged signal with the function explicitly to change the button text. And also you don't need to emit a valueChanged signal with m_iValue. Make the following changes to the appropriate files

QML:

 Button { horizontalAlignment: HorizontalAlignment.Center verticalAlignment: VerticalAlignment.Center id: aButton text: _app.value onClicked: { _app.value = _app.value + 1 } } 

HPP:

 signals: void valueChanged(); 

CPP:

fix value Changed ();

+10
source

All Articles