QML 信号和处理程序事件系统

本文深入探讨QML中信号和事件系统的应用,包括信号处理程序的定义与使用,属性更改信号处理,信号的连接与断开,以及自定义信号的创建。通过实例展示如何在不同场景下利用信号进行对象间通信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

信号和处理程序事件系统

应用程序和用户界面组件需要使用信号和信号处理程序相互通信。发送信号就会调用其相关联的处理逻辑进行处理。

使用信号处理程序接收信号

为了在特定对象发出特定信号时接收通知,对象定义应声明一个名为on <Signal>的信号处理程序,其中<Signal>信号的名称,首字母大写。信号处理程序应包含在调用信号处理程序时要执行的JavaScript代码。

import QtQuick 2.14
import QtQuick.Controls 2.14

Rectangle {
    id: rect
    width: 250; height: 250

    Button {
        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        text: "Change color!"
        onClicked: {  //点击按钮时发送clicked信号,定义onClicked进行处理
            rect.color = Qt.rgba(Math.random(), Math.random(), Math.random(), 1);
        }
    }
}

属性更改信号处理程序

当对象的属性发生变化时,QML引擎会自动为对象添加属性更改信号,并发送信号,这些信号的信号处理程序以on <Property> Changed的形式编写,其中<Property>属性的名称,首字母大写。

import QtQuick 2.14

Rectangle {
    id: rect
    width: 100; height: 100

    TapHandler {
        //点击屏幕时,修改了pressed属性,触发onPressedChanged
        onPressedChanged: console.log("taphandler pressed?", pressed)
    }
}

使用 Connections关联信号

在某些情况下,可能希望访问发出该信号的对象之外的信号。为此,该QtQuick模块提供用于连接任意对象信号的Connections类型。可以接收来自它的指定的任何信号的目标

import QtQuick 2.14
import QtQuick.Controls 2.14

Rectangle {
    id: rect
    width: 250; height: 250

    Button {
        id: button
        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        text: "Change color!"
    }

    Connections {  //关联button的clicked信号
        target: button
        onClicked: {
            rect.color = Qt.rgba(Math.random(), Math.random(), Math.random(), 1);
        }
    }
}

附加信号处理

从附加类型接收一个信号并进行处理

import QtQuick 2.14

Rectangle {
    width: 200; height: 200
    color: Qt.rgba(Qt.random(), Qt.random(), Qt.random(), 1)

    Component.onCompleted: {  //Component 附加类型
        console.log("The rectangle's color is", color)
    }
}

自定义类型添加信号

可以通过signal关键字将信号添加到自定义QML类型。

定义新信号的语法为:

signal <name>[([<type> <parameter name>[, ...]])]

示例

// SquareButton.qml
import QtQuick 2.14

Rectangle {
    id: root

    signal activated(real xPosition, real yPosition)  //定义一个信号 
    property point mouseXY
    property int side: 100
    width: side; height: side

    TapHandler {
        id: handler
        onTapped: root.activated(mouseXY.x, mouseXY.y)  //发送信号
        onPressedChanged: mouseXY = handler.point.position
    }
}

// myapplication.qml
SquareButton {
//处理activated信号
    onActivated: console.log("Activated at " + xPosition + "," + yPosition)
}

信号连接方法和信号

信号对象使用connect()连接方法或另一个信号。当信号连接到方法时,只要发出信号,该方法就会自动调用。这种机制使信号可以通过方法而不是信号处理程序来接收

import QtQuick 2.14

Rectangle {
    id: relay

    signal messageReceived(string person, string notice)  //定义信号

    Component.onCompleted: {
        relay.messageReceived.connect(sendToPost)   //关联信号
        relay.messageReceived.connect(sendToTelegraph)
        relay.messageReceived.connect(sendToEmail)
        relay.messageReceived("Tom", "Happy Birthday")
    }

    function sendToPost(person, notice) {
        console.log("Sending to post: " + person + ", " + notice)
    }
    function sendToTelegraph(person, notice) {
        console.log("Sending to telegraph: " + person + ", " + notice)
    }
    function sendToEmail(person, notice) {
        console.log("Sending to email: " + person + ", " + notice)
    }
}

使用disconnect()断开信号

Rectangle {
    id: relay
    //...

    function removeTelegraphSignal() {
        relay.messageReceived.disconnect(sendToTelegraph)
    }
}

信号关联信号

import QtQuick 2.14

Rectangle {
    id: forwarder
    width: 100; height: 100

    signal send()
    onSend: console.log("Send clicked")

    TapHandler {
        id: mousearea
        anchors.fill: parent
        onTapped: console.log("Mouse clicked")
    }

    Component.onCompleted: {
        mousearea.tapped.connect(send)   //关联信号 
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值