Search code examples
qtqmlqtquick2qtquickcontrols

Cannot access elements of the container


I have a QML object as follows:

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

    Text {
        id: textSingleton
    }

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

I have another control that is contained within it and my issue is that I cannot access the playCalSound or textSingleton elements.

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

   view.textSingleton // view is not defined
}

Solution

  • You should define properties for objects you want use outside file. Try this

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