本页内容

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` 来指定信号处理程序,就像直接在目标对象中指定它们一样。但不建议这样做。如果您以这种方式指定了一个信号处理程序,则同一 `Connections` 对象中所有作为 `function ` 指定的信号处理程序都将被忽略。

另请参阅 Qt Qml。

属性文档

enabled : bool

该属性用于指定该项是否接受更改事件。

默认情况下,该属性的值为true 。

ignoreUnknownSignals : bool

通常情况下,连接到不存在的信号会引发运行时错误。

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

target : QtObject

该属性保存发送信号的对象。

如果未设置此属性,则target 默认设置为Connection的父对象。

如果设置为 null,则不会建立连接,并且在目标不为 null 之前,将忽略所有信号处理程序。

© 2026 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.