Search code examples
qtqmlqt5qtquick2qtquickcontrols2

Qt QML: What's the exact meaning of the property of enabled of Connections


I am currently using Qt 5.8.0 64bit on VS2015, Windows 10 64bit. According to the doc, the type Connections has gained a new property as enabled since 5.7.0. The doc says:

This property holds whether the item accepts change events.

I guess this property controls whether the connections are valid, right? However, when I turn off this property, and the connections are still working! Demo code is listed below:

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Button{
        id: button
        anchors.centerIn: parent
        width: 100
        height: 50
        text: "Click!"
    }

    Connections{
        target: button
        enabled: false
        onClicked:{
            console.log("button Clicked!");
        }
    }
}

"button Clicked!" is still running out from the debug output! What's the exact meaning of the property "enabled"?

P.S.: it turns out if I set "enabled" as true (the default value is also true), and turn it off Component.onCompleted, the connections become invalid and the debug console won't print "button Clicked!" anymore when clicking the button:

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Button{
        id: button
        anchors.centerIn: parent
        width: 100
        height: 50
        text: "Click!"
    }

    Connections{
        id: connections
        target: button
        enabled: true
        onClicked:{
            console.log("button Clicked!");
        }
    }
    Component.onCompleted: connections.enabled = false;
}

Is it a bug?


Solution

  • Yes, you have stumbled upon a bug, the initial value of the enabled property is ignored. enabled is only taken into account if the value is changed after the Connections item has been completely initalized. Therefore your Component.onCompleted trick is a nice workaround.

    I have fixed the issue at https://codereview.qt-project.org/#/c/194840/.