Reputation: 3806
I have a C++ implemented Quick Item and it offeres multiple properties and Q_INVOKABLE methods with return values. Most of these methods depend on these properties.
Can I define a notify signal for methods? Or when binding to it, can I add additional dependencies so the method is evaluated again?
In this example I want all text items to be updated whenever theItem.something
changes.
SimpleCppItem {
id: theItem
something: theSpinBox.value
}
RowLayout {
SpinBox { id: theSpinBox; }
Repeater {
model: 10
Text { text: theItem.computeWithSomething(index) }
}
}
The implementation of the SimpleCppItem
looks like this:
class SimpleCppItem : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(int something READ something WRITE setSomething NOTIFY somethingChanged)
public:
explicit SimpleCppItem(QQuickItem *parent = Q_NULLPTR) :
QQuickItem(parent),
m_something(0)
{ }
Q_INVOKABLE int computeWithSomething(int param)
{ return m_something + param; } //The result depends on something and param
int something() const { return m_something; }
void setSomething(int something)
{
if(m_something != something)
Q_EMIT somethingChanged(m_something = something);
}
Q_SIGNALS:
void somethingChanged(int something);
private:
int m_something;
};
Upvotes: 3
Views: 722
Reputation: 1639
That is not possible with functions. But there are some workarouds:
"Small Hack" (You get warning M30: Warning, Do not use comma expressions Thanks to GrecKo, no warning anymore!
Repeater {
model: 10
Text {
text: {theItem.something; return theItem.computeWithSomething(index);}
}
}
Or you connect every item in the repeater with the "somethingChanged" signal:
Repeater {
model: 10
Text {
id: textBox
text: theItem.computeWithSomething(index)
Component.onCompleted: {
theItem.somethingChanged.connect(updateText)
}
function updateText() {
text = theItem.computeWithSomething(index)
}
}
}
===== ORIGNAL QUESTION =====
You can catch the signal in the QML file like this:
SimpleCppItem {
id: theItem
something: theSpinBox.value
onSomethingChanged() {
consoloe.log("Catched: ",something)
//something ist the name of the parameter
}
}
Upvotes: 1