无法访问容器的元素

时间:2016-02-01 08:44:32

标签: qt qml qtquick2 qtquickcontrols

我有一个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
}

1 个答案:

答案 0 :(得分: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'
}