2017-11-13 3 views
0

の内部コンポーネントのオブジェクトを作成します(外部オブジェクトが作成された後)であるこのような何かすることが可能です外成分の物体

import QtQuick 2.7 
import QtQuick.Window 2.2 

Window{ 
    id: root_ 
    visible: true 
    width: 300 
    height: 300 

    Component { 
     id:compouter 

     Column{ 
      anchors.fill: parent 

      Component { 
       id: compinner 

       Rectangle { 
        width:parent.width 
        height:parent.height/2 
       } 
      } 
     } 
    } 

    Component.onCompleted: { 

     var c = compouter.createObject(this) 
     //var d = c.compinner.createObject(c, {"color": "green"}) 
     //var e = c.compinner.createObject(c, {"color": "red"}) 
    } 
} 

を、私は外側のオブジェクト内の複数のオブジェクトを作成したいです。しかし、私はエラーが発生するので、これは可能ではありません: TypeError: Cannot call method 'createObject' of undefined

この問題を回避する方法はありますか?外側のオブジェクトのインスタンス化中にすべての内側のオブジェクトをインスタンス化することは、おそらく可能でしょうか?

答えて

0

idは、関数呼び出しの対象外です。

あなたはどちらかあなたのオブジェクトを作成し、あなたの外側のコンポーネントに機能を追加することができます。

Component { 
    id:compouter 

    Column{ 
     anchors.fill: parent 

     function createCompinner(arg) { compinner.createObject(this, arg) } 

     Component { 
      id: compinner 

      Rectangle { 
       width:parent.width 
       height:parent.height/2 
      } 
     } 
    } 
} 

かプロパティとして内部Component公開:

Component { 
    id:compouter 

    Column{ 
     property alias compinner: compinnerComponent 
     anchors.fill: parent 

     Component { 
      id: compinnerComponent 

      Rectangle { 
       width:parent.width 
       height:parent.height/2 
      } 
     } 
    } 
} 

をし、そのようにアクセスします。

+0

はい、内部コンポーネントのエイリアスプロパティを作成するというアイディアは、 – FloriHe

0

[OK]を、私はoutterオブジェクトに信号を送って、回避策を見つけました:

import QtQuick 2.7 
import QtQuick.Window 2.2 

Window{ 
    id: root_ 
    visible: true 
    width: 300 
    height: 300 

    Component { 
     id:compouter 

     Column{ 
      anchors.fill: parent 
      signal createRect(var color) 

      onCreateRect: { 

       compinner.createObject(this, {"color": color}) 
      } 

      Component { 
       id: compinner 

       Rectangle { 
        width:parent.width 
        height:parent.height/2 
       } 
      } 
     } 
    } 

    Component.onCompleted: { 

     var c = compouter.createObject(this) 
     c.createRect("green") 
     c.createRect("red") 
    } 

は、それが働いていますが、多分これにはいくつかのより一般的なアプローチは、(受け入れ答えを参照)があります。ポスターが示唆しているように、関数を呼び出すのは、信号を送るよりも意味的にクリーンです。

関連する問題