Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How To Accessing Data Between QML Objects?



  • Hello guys.
    I have a question about accessing possibilities between QML objects.

    page{
    ....
        ListView{
            id: listview
            anchors.bottom: parent.bottom
            anchors.right: parent.right
            width: parent.width / 2
            height: parent.height / 2
            clip: true
            property Item header_component
            header: Rectangle{
                id: header_rectangle
                z: 2
                width: parent.width
                height: textID.implicitHeight
                color: "Orange"
                //Component.onCompleted: console.log(parent.width)
                Text{id:textID ;text: language_selector_alias.currentIndex === 0 ? qsTr("NACA " + chart.naca_model_on_chart_header + " Point Coordinates") : qsTr(" مختصات نقاط ناکا" + chart.naca_model_on_chart_header); font.family: font_of_options; font.pixelSize: 30; color: "White"; anchors.horizontalCenter: parent.horizontalCenter}
            }
            ScrollBar.vertical: ScrollBar {
                id: control
                //y: header_component.height
                height: 2
                size: 0.3
                position: 0.2
                active: true
                orientation: Qt.Vertical
                contentItem: Rectangle {
                    implicitWidth: 6
                    implicitHeight: 100
                    radius: width / 2
                    color: control.pressed ? "White" : "Green"
                }
            }
    .....
    }
    

    I want to access to header_rectangle properties in control (scrollBar) object but QML does not allow me to do that!
    I even try to put header_rectangle in Component but the problem still persist!
    Also I try global variables (manipulate them in component.onCompleted signal) with no luck!
    Can anyone point me to right direction?
    Thanks in advance.



  • hi @Nima-Ghorab
    listview.headerItem.height



  • @LeLev thank you so much it works like a charm.
    I have just one more question.
    How can I access to properties of a Component object outside of itself in following code:

        Component{
            id: comp
            Rectangle{
                id: rect_ID
                z: 2
                width: parent.width
                height: textID.implicitHeight
                color: "Orange"
                Component.onCompleted: console.log(page.width)
                Text{id:textID ;text: language_selector_alias.currentIndex === 0 ? qsTr("NACA " + chart.naca_model_on_chart_header + " Point Coordinates") : qsTr(" مختصات نقاط ناکا" + chart.naca_model_on_chart_header); font.family: font_of_options; font.pixelSize: 30; color: "White"; anchors.horizontalCenter: parent.horizontalCenter}
            }
        }
        Rectangle{
            id:???
            color: "Orange"
            //width: i
            Component.onCompleted: console.log(comp.width)
        }
    

    As far as I know, component just a inline encapsulation and we need to instantiate it in order to use its properties.
    Correct me if I'm wrong.
    Thank you so much.



  • hi
    @Nima-Ghorab said in How To Accessing Data Between QML Objects?:

    As far as I know, component just a inline encapsulation and we need to instantiate it in order to use its properties.

    not exactly see http://doc.qt.io/qt-5/qml-qtqml-component.html



  • create an instance of your component with a Loader



  • @LeLev thank you so much.


Log in to reply