Connections QML Type

描述与信号的通用连接。更多

Import Statement: import QtQml

属性

详细说明

Connections 对象创建与 QML 信号的连接。

当连接到 QML 信号时,通常的方法是创建一个 "on<Signal>" 处理程序,当收到信号时作出反应,就像这样:

MouseArea {
    onClicked: (mouse)=> { foo(mouse) }
}

然而,在某些情况下,如当以下情况时,不可能用这种方法连接信号:

  • 需要对同一信号建立多个连接
  • 在信号发送者的范围之外创建连接
  • 连接到 QML 未定义的目标

需要上述任何一种情况时,可使用 Connections 类型来代替。

例如,上面的代码可以改成使用 Connections 对象,就像这样:

MouseArea {
    Connections {
        function onClicked(mouse) { foo(mouse) }
    }
}

更一般地说,Connections 对象可以是信号发送者之外的某个对象的子对象:

MouseArea {
    id: area
}
// ...
Connections {
    target: area
    function onClicked(mouse) { foo(mouse) }
}

注意: 为了向后兼容,您也可以指定信号处理程序,而无需function ,就像在目标对象中直接指定一样。但不建议这样做。如果以这种方式指定了一个信号处理程序,那么在同一连接对象中指定为function 的所有信号处理程序都将被忽略。

另请参见 Qt Qml.

属性文档

enabled : bool

此属性表示项目是否接受更改事件。

默认情况下,该属性为true


ignoreUnknownSignals : bool

通常,连接到不存在的信号会产生运行时错误。

如果将此属性设置为true ,则会忽略此类错误。如果您打算连接不同类型的对象,为每个对象处理不同的信号集,这将非常有用。


target : QtObject

该属性包含发送信号的对象。

如果未设置该属性,target 默认为连接的父对象。

如果设置为空,则不会建立连接,任何信号处理器都会被忽略,直到目标不为空为止。


© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.