资讯详情

qml 信号和处理程序事件系统(Signal and Handler Event System)

目录标题

  • 概述
  • 信号处理程序
  • 属性变更信号处理程序的属性
  • 信号参数
  • 关联信号(Connections)
  • 附加信号处理
  • 添加自定义类型的信号
  • 信号连接方法/信号
    • 使用disconnect()断开信号
  • 信号关联信号


概述

<AttachingType>.on<Signal> //<Signal>是信号的名称,首字母必须大写 ///我们在处理程序中编写我们想要实现的效果  on<Property>Changed ////属性改变信号处理程序的用法 //<Property>是属性的名称,首字母大写  ///都放在当前元素的内部 

信号处理程序

import QtQuick  import QtQuick.Controls  Rectangle { 
              id: rect     width: 250; height: 250      Button { 
                  anchors.bottom: parent.bottom         anchors.horizontalCenter: parent.horizontalCenter         text: "Change color!"         onClicked: { 
                      rect.color = Qt.rgba(Math.random(), Math.random(), Math.random(), 1);
        }
    }
  }

属性更改信号处理程序

import QtQuick

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

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

信号参数

// Status.qml import QtQuick
//注意:函数中形式参数的名称不必与信号中的名称相匹配。
Item { 
         
    id: myitem
    signal errorOccurred(message: string, line: int, column: int) } 
Status { 
         
    onErrorOccurred: (mgs, line, col) => console.log(`${ 
           line}:${ 
           col}: ${ 
           msg}`) 
  //不需要参数时 onErrorOccurred: function (message) { console.log(message) }
    }

关联信号(Connections)

import QtQuick
import QtQuick.Controls

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
        function 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 <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)
     }

信号连接方法/信号


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)   //关联信号 
    }

标签: mgs203磁性接近传感器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台