2017-07-05 32 views
2

我有一个C++实现的快速项目,它出现多个属性和Q_INVOKABLE方法与返回值。这些方法大部分取决于这些属性。如何判断有关其他依赖关系的qml绑定?

我可以为方法定义通知信号吗?或者当绑定到它时,我可以添加额外的依赖项,以便再次评估该方法吗?

在此示例中,我希望每当theItem.something发生更改时都要更新所有文本项目。

Screenshot of my example application

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; 
}; 

回答

1

那是不可能的功能。但也有一些workarouds:

“小黑客”(你得到警告M30:警告,不要使用逗号表达式感谢GrecKo,没有警告了

Repeater { 
     model: 10 
     Text { 
      text: {theItem.something; return theItem.computeWithSomething(index);} 
     } 
    } 

或者你每一个项目的连接!中继器与 “somethingChanged” 信号:

Repeater { 
    model: 10 
    Text { 
     id: textBox 
     text: theItem.computeWithSomething(index) 
     Component.onCompleted: { 
      theItem.somethingChanged.connect(updateText) 
     } 
     function updateText() { 
      text = theItem.computeWithSomething(index) 
     } 
    } 
} 

=====一部开拓创新的问题=====

可以赶上SI gnal在这样的QML文件中:

SimpleCppItem { 
    id: theItem 
    something: theSpinBox.value 

    onSomethingChanged() { 
     consoloe.log("Catched: ",something) 
     //something ist the name of the parameter 
    } 
} 
+0

因此,我需要更新所有使用该功能的文本项目?如果我不知道谁使用它(因为用在单独的项目文件中)会怎么样?我宁愿定义绑定本身被定义的地方。 –

+0

更改之后:我不认为我可以为'computeWithSomething'的每个参数组合实现一个单独的Q_PROPERTY。在我的情况下,该方法是从Repeater内部使用索引作为参数调用的。 –

+3

你可以通过这样做来避免警告:'{theItem.something;返回theItem.computeWithSomething(index);}' – GrecKo

相关问题