2

我目前在 VS2015、Windows 10 64 位上使用 Qt 5.8.0 64 位。根据文档,该类型从 5.7.0Connections开始获得了新属性。enabled医生说:

此属性保存项目是否接受更改事件。

我猜这个属性控制连接是否有效,对吧?但是,当我关闭此属性时,连接仍在工作!演示代码如下:

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!");
        }
    }
}

“点击按钮!” 仍然从调试输出中耗尽!属性“启用”的确切含义是什么?

PS:事实证明,如果我将“启用”设置为真(默认值也是真),然后将其关闭Component.onCompleted,连接将变为无效,并且调试控制台不会打印“按钮单击!” 单击按钮时不再:

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

它是一个错误吗?

4

1 回答 1

3

是的,您偶然发现了一个错误,该enabled属性的初始值被忽略了。仅在项目完全初始化enabled后更改值时才考虑。Connections因此,您的Component.onCompleted技巧是一个很好的解决方法。

我已经在https://codereview.qt-project.org/#/c/194840/解决了这个问题。

于 2017-05-16T20:28:37.020 回答