【问题标题】:how to disconnect the method for a signal in qml?如何断开qml中信号的方法?
【发布时间】:2017-11-13 18:29:52
【问题描述】:

在QML中,在某些情况下,我需要删除信号的旧方法并重新定义一个新的方法来处理信号,演示如下:

import QtQuick 2.6
import QtQuick.Window 2.2

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

    MouseArea {
        id:mouse
        anchors.fill: parent
        onClicked: {
            console.log("11111");
        }
    }


    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }

    Component.onCompleted: {
        //if(false){
        //}
        // mouse.clicked = null;
        //mouse.clicked.disconnect();

        mouse.clicked.connect(
                    function(){
                        console.log("22222")
                    });
    }

}

我连接了一个新函数,但是它会显示“11111”和“22222”,似乎它添加了一个新函数来处理信号。

我试过设置“mouse.clicked = null”,还是不行。

我也试过断开方法,如下:

MouseArea {
    id:mouse
    anchors.fill: parent
    onClicked:say()
}

Component.onCompleted: {
    mouse.clicked.disconnect(say);
    mouse.clicked.connect(
                function(){
                    console.log("22222")
                });
}

function say(){
    console.log("11111");
}

仍然打印“11111”和“22222”,如何删除旧方法并重新定义Component.onCompleted()中的方法?

2017 年 6 月 13 日更新:

谢谢,伙计们。我也使用连接,仍然无法断开连接,因为我使用 qt5.6.1,没有启用属性:(。如果我们使用显式连接()函数,它可以工作。但就我而言,我想我can't connect a method ,我需要检查一些 If 情况来定义是否需要删除 Component.onCompleted 中的信号处理程序。

还有一个小问题,为什么 window 组件的完成速度比 mouseArea 快?日志“首次加载”首先加载。

  MouseArea {
        id:mouse
        anchors.fill: parent
        Connections{
            onClicked:say()
        }
        Component.onCompleted: {
            //mouse.clicked.connect(say)
            console.log("second load")
        }
    }

    Component.onCompleted: {
        console.log("first load")
        mouse.clicked.connect(
                    function(){
                        mouse.clicked.disconnect(say);
                        //mouse.clicked.disconnect(say);
                        console.log("22222")
                    });

    }

【问题讨论】:

    标签: qt qml


    【解决方案1】:

    您无法断开默认信号处理程序onSignal: { do what ever }

    只有当您事先与onSignal.connect(say) 连接时,您才能断开连接。您可以在 Component.onCompleted 中执行此操作。
    如果使用语法:

    onSignal: say()
    

    这等于

    onSignal.connect(function() { say() })
    

    因此您不能使用onSignal.disconnect(say),因为从未连接过say。 (我不知道它是连接到onSignal 还是signal - 两者都可以手动完成,或者它是否根本没有连接。你不能断开它,没有对隐式创建的引用功能。)


    在 QML 中,更好的方法是保持声明性。为此,您可以使用Connections-construct:

    Connections {
        target: mouse // set to null to disconnect (Qt<=5.6)
        onClicked: { do what ever }
        enabled: true // change this, when you want to disconnect it (Qt>=5.7)
    } 
    

    【讨论】:

    • hi derM,Connections 还是不行,我用 Qt5.6.1 试了一下,没有 :enabled 属性。
    • 哦,好的。我没有看到,这个属性是在 Qt5.7 (doc.qt.io/qt-5/qml-qtqml-connections.html#enabled-prop) 中引入的。
    • 我在我的代码中添加了注释。您可以在 Qt5.6 及更早版本中将target 设置为null 以停用Connection
    【解决方案2】:

    您一开始没有使用显式的connect() 函数。要disconnect() 一个函数,它必须已使用connect() 显式连接。

    这里有一个例子可能会有所帮助:

    http://doc.qt.io/qt-5/qtqml-syntax-signals.html#connecting-signals-to-methods-and-signals

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多