2017-07-05 5 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の警告を取得:!、

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

GrecKo、もう警告なしにコンマ式に感謝を使用するか、またはあなたがすべてのアイテムを接続しないでください警告あなたは、Siをキャッチすることができます

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

=====のorignalのQUESTION =====

:信号を "somethingChanged" とリピータQMLファイルのgnalは次のようになります。

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

関連する問題