无法访问容器的元素

Cannot access elements of the container

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

您应该为要在文件外部使用的对象定义属性。试试这个

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'
}