2
我有一个C++实现的快速项目,它出现多个属性和Q_INVOKABLE方法与返回值。这些方法大部分取决于这些属性。如何判断有关其他依赖关系的qml绑定?
我可以为方法定义通知信号吗?或者当绑定到它时,我可以添加额外的依赖项,以便再次评估该方法吗?
在此示例中,我希望每当theItem.something
发生更改时都要更新所有文本项目。
SimpleCppItem {
id: theItem
something: theSpinBox.value
}
RowLayout {
SpinBox { id: theSpinBox; }
Repeater {
model: 10
Text { text: theItem.computeWithSomething(index) }
}
}
的SimpleCppItem
的实现看起来是这样的:
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;
};
因此,我需要更新所有使用该功能的文本项目?如果我不知道谁使用它(因为用在单独的项目文件中)会怎么样?我宁愿定义绑定本身被定义的地方。 –
更改之后:我不认为我可以为'computeWithSomething'的每个参数组合实现一个单独的Q_PROPERTY。在我的情况下,该方法是从Repeater内部使用索引作为参数调用的。 –
你可以通过这样做来避免警告:'{theItem.something;返回theItem.computeWithSomething(index);}' – GrecKo