2016-02-01 111 views
1

的元素我有一個QML對象如下:無法訪問容器

// File: ControlView.qml 
Rectangle { 
    id: view 
    property bool darkBackground: false 

    Text { 
     id: textSingleton 
    } 

    SoundEffect { 
     id: playCalSound 
     source: "qrc:/sound/calender_time_camera.wav" 
    } 
} 

我有一個是包含在它的另一個控制,我的問題是,我無法訪問playCalSoundtextSingleton元素。

// File: MyControl.qml 
ControlView { 
    .... 
    playCalSound.play() // playCalSound is not defined 
    textSingleton.font.pixelSize // textSingleton is not defined 

    view.textSingleton // view is not defined 
} 

回答

3

您應該爲要在文件外部使用的對象定義屬性。試試這個

Rectangle { 
    id: view 
    property bool darkBackground: false 
    property var effect: playCalSound 
    property alias singletext: textSingletone.text 

    Text { 
     id: textSingleton 
    } 

    SoundEffect { 
     id: playCalSound 
     source: "qrc:/sound/calender_time_camera.wav" 
    } 
} 

.............. 


ControlView { 
    .... 
    Component.onComplited: effect.play() 

    singletext: 'some text' 
} 
+0

太棒了!非常感謝! – Luca