我有一个QML对象如下:
// File: ControlView.qml
Rectangle {
id: view
property bool darkBackground: false
Text {
id: textSingleton
}
SoundEffect {
id: playCalSound
source: "qrc:/sound/calender_time_camera.wav"
}
}
我有另一个控件包含在其中,我的问题是我无法访问playCalSound
或textSingleton
元素。
// File: MyControl.qml
ControlView {
....
playCalSound.play() // playCalSound is not defined
textSingleton.font.pixelSize // textSingleton is not defined
view.textSingleton // view is not defined
}
答案 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'
}