2016-04-12 27 views
0

を結合私は財産幅の矩形を持っていた場合、私は混乱値を設定するための3つのオプションがあります。QMLのプロパティ - 読み取り専用、割り当てを、

read-only property int widthReadOnly: 200 

Rectangle{ 
    width: 200        //first 
    width: widthReadOnly     //second 
    Component.onCompleted: {width = 200} //third 
} 

がそれぞれを使用するとき、あなたは私に言うことができますそのうちの? ありがとうございます。

答えて

1

最初はすべての例が全く同じです。 最初と2番目のexamleは値へのバインディングを作成しますが、これらはint(最初)または決して変更されない読み取り専用プロパティ(2番目)です。変更された値が変更された場合、ではなく、が譲受人を変更します)。

バインディングを使用するのは、変更可能な値(親アイテムの幅など)にバインドする場合です。親幅が変更された場合だから、子アイテムに伝播されます。

import QtQuick 2.0 
import QtQuick.Controls 1.4 
Item { 
    Rectangle { 
     id: papa 
     width: 100 
     height: 100 
     color: "red" 

     Rectangle { 
      id: child 
      anchors.centerIn: parent 
      width: parent.width/2 
      height: parent.height/2 
      color: "lightsteelblue" 
     } 
    } 
    Button { 
     id: button 
     onClicked: { 
      papa.width = 100 + (Math.random() * 100) 
     } 
    } 
} 

ご覧のとおり、パパ幅が更新された場合、それはまた、子アイテムの幅を変更します。

関連する問題