inheritanceqmlqt5.3

QML enabled property and inheritance : disable parent and enable child?


I have a MyInnerRectangle component inside a MyRectangle. I want to be able to set MyInnerRectangle.enabled to true even if MyRectangle.enabled is set to false.

Is that possible ? If yes how ?

I may have misunderstood how enabled property works.

I tried this :
Disclaimer, these files may not work properly and I work with Qt 5.3.2 (an old version).

// MyInnerRectangle.qml
import QtQuick 2.3
import UI_Components 1.0

Rectangle {
    color: "red"
    Component.onCompleted: {
        console.log("InnerRectangle : enabled: " + enabled + ", visible: " + visible)
    }
}
// MyRectangle.qml
import QtQuick 2.3
import UI_Components 1.0

Rectangle {
    property alias innerEnabled: innerRectangle.enabled

    color: "green"
    anchors.fill: parent

    MyInnerRectangle {
        id: innerRectangle

        visible: enabled

        width: parent.height/2
        height: parent.height/2
        anchors.centerIn: parent
    }
}

In the main.qml (this file may not be complete, as it is a part of my app)

// in main.qml
MyRectangle {
    enabled : false
    innerEnabled: true

    anchors.fill: parent
}

But this will display :

 qml: InnerRectangle : enabled: false, visible: false

When I expect true


Solution

  • The docs are pretty clear on this:

    Setting this property directly affects the enabled value of child items. When set to false, the enabled values of all child items also become false. When set to true, the enabled values of child items are returned to true, unless they have explicitly been set to false.

    So if MyRectangle is disabled, its children must be disabled too. My suggestion if you want to break that link between parent/child is to simply make up a new property that you have complete control over. So instead of setting enabled, set myEnabled or whatever you want to call it.