QML中的信号和函数

来源:互联网 发布:超声波传感器淘宝 编辑:程序博客网 时间:2024/05/16 04:48

Qt C++中,signals & slots是其核心机制,是应用程序与UI组件交互的神经网络,同样在QML中,也有类似的signal & handler机制,signal信号就是我们常说的一种事件,可通过signal handler信号处理器来处理这个信号。当某个signal信号被发送时,对应的handler信号处理器也被触发,我们就可以在handler信号处理器中做一些事情,来处理这个signal信号事件。

1、“HelloWorld”信号简述

下面是一个简单的例子:

[plain] view plain copy
  1. import QtQuick 2.3  
  2. import QtQuick.Controls 1.2  
  3.   
  4. Rectangle {  
  5.     width: 360; height: 200  
  6.     color: "lightblue"  
  7.   
  8.     Button {  
  9.         text: "Quit!"  
  10.         anchors.centerIn: parent  
  11.         onClicked: Qt.quit()  
  12.     }  
  13. }  

在一个Rectangle中有一个Button,这个Button默认有一个clicked()信号,相应的就会有一个onClicked信号处理器。如果有一个signal()信号,那么相应的信号处理器handler就是onSignal,注意大小写,信号中有参数时信号处理器会接收到且可以使用。当我们用鼠标左键点击例子中的的Button时,就会发送clicked()信号,触发信号处理器onClicked,这里调用了Qt.quit()函数来退出应用程序。

2、属性handler

QML属性值改变时,会自动发送一个该属性值变化的信号,对应的信号处理器的格式是on<Property>Changed,不管是QML内建的属性,还是我们自定义的属性,一旦属性值改变,都会触发相应的信号处理器,举例如下:

[plain] view plain copy
  1. import QtQuick 2.3  
  2. import QtQuick.Controls 1.2  
  3.   
  4. Rectangle {  
  5.     width: 360; height: 100  
  6.     color: "lightblue"  
  7.   
  8.     onColorChanged: console.log("color changed")  
  9.   
  10.     Button {  
  11.         text: "Change Color"  
  12.         anchors.centerIn: parent  
  13.         onClicked: parent.color = "yellow"  
  14.     }  
  15. }  

点击Button按钮时,Rectanglecolor会从原来的lightblue变为yellow,这时就会触发onColorChanged,输出“color changed”

3、附加signalhandler

附加信号处理器接收的信号来自附加元素类型,而不是当前对象,例如下面例子中的KeysComponent

[plain] view plain copy
  1. import QtQuick 2.3  
  2.   
  3. Rectangle {  
  4.     id: rect  
  5.     width: 360; height: 100  
  6.   
  7.     focus: true  
  8.     Keys.onSpacePressed: rect.color = Qt.rgba(Math.random(), Math.random(), Math.random(), 1)  
  9.   
  10.     Component.onCompleted: rect.color = "green"  
  11. }  

上述例子中我们使用了Component.onCompleted,在组件完成时设置Rectangle的颜色为绿色,还使用了Keys.onSpacePressed,按下空格键时随机改变Rectangle的颜色。MathJavaScript的对象,在这里我们可以直接使用,因为QML是对JavaScript的一种扩展,支持JavaScript的大部分东西。

4、使用Connections连接handler

在前面的例子中,我们对某个信号signal,直接在当前对象下使用了on<Signal>形式的信号处理器,但是在某些情况下,我们不得不使用Connections来连接一个信号,比如说,多个对象连接到同一个信号时,在发射信号的对象作用域之外建立连接时,连接不是QML定义的对象(如在C++中定义的对象)时,举例说明:

[plain] view plain copy
  1. import QtQuick 2.3  
  2.   
  3. Item {  
  4.     width: 500; height: 150  
  5.   
  6.     Row {  
  7.         spacing: 50  
  8.   
  9.         Rectangle {  
  10.             width: 100; height:100  
  11.             color: "red"  
  12.   
  13.             MouseArea {  
  14.                 anchors.fill: parent  
  15.                 onClicked: parent.color=  
  16.                         Qt.rgba(Math.random(), Math.random(), Math.random(), 1)  
  17.             }  
  18.         }  
  19.   
  20.         Rectangle {  
  21.             id: rectYellow  
  22.             width: 100; height:100  
  23.             color: "yellow"  
  24.   
  25.             MouseArea {  
  26.                 anchors.fill: parent  
  27.                 Connections {  
  28.                     onClicked: rectYellow.color=  
  29.                         Qt.rgba(Math.random(), Math.random(), Math.random(), 1)  
  30.                 }  
  31.             }  
  32.         }  
  33.   
  34.         Rectangle {  
  35.             id: rectGreen  
  36.             width: 100; height:100  
  37.             color: "green"  
  38.   
  39.             MouseArea {  
  40.                 id: mouseArea  
  41.                 anchors.fill: parent  
  42.             }  
  43.   
  44.             Connections {  
  45.                 target: mouseArea  
  46.                 onClicked: rectGreen.color=  
  47.                         Qt.rgba(Math.random(), Math.random(), Math.random(), 1)  
  48.             }  
  49.         }  
  50.     }  
  51. }  

上述例子,列举了三种用法。第一种、第二种用法类似,都是直接在MouseArea作用域内连接,可以看出第二种方法的Connections可以省略,MouseArea指定了一块区域来处理鼠标事件,第三种方法是在MouseArea作用域之外建立连接,也就不得不用Connections,三种方法都是在鼠标点击时随机改变Rectangle的颜色。

5、自定义信号、函数

QML中,我们可以自定义信号和函数,首先需要声明的是,不能像C++那样支持重载,语法如下:

[plain] view plain copy
  1. signal <signalName>[([<type> <parameter name>[, …]])]  
  2. function <functionName>([<parameterName>[, ...]]) { <body> }  

自定义的信号和函数都可以带参数,不同的是,信号要指明参数类型,而函数却用不着,这是为什么呢,因为函数的参数类型默认为万能的var类型,而且也没有像C++那样必须指明返回值类型,下面是一个综合的例子:

[plain] view plain copy
  1. import QtQuick 2.3  
  2.   
  3. Item {  
  4.     width: 600; height: 100  
  5.   
  6.     Rectangle {  
  7.         id: rect1  
  8.         width: 100; height: 100  
  9.         color: "lightblue"  
  10.   
  11.         Text {  
  12.             id: text1  
  13.             anchors.centerIn: parent  
  14.             text: "custom \n signal"  
  15.         }  
  16.     }  
  17.   
  18.     Rectangle {  
  19.         id: rect2  
  20.         width: 100; height: 100  
  21.         anchors.right: parent.right  
  22.         color: "lightblue"  
  23.   
  24.         function changeColor(obj) {  
  25.             obj.color = Qt.rgba(Math.random(), Math.random(), Math.random(), 1)  
  26.         }  
  27.   
  28.         Text {  
  29.             id: text2  
  30.             anchors.centerIn: parent  
  31.             text: "custom \n funtion"  
  32.         }  
  33.     }  
  34.   
  35.     Component {  
  36.         id: colorComponent  
  37.   
  38.         Rectangle {  
  39.             id: colorMaker  
  40.             width: 100; height:100  
  41.   
  42.             signal colorMade(color col)  
  43.   
  44.             MouseArea {  
  45.                 anchors.fill: parent  
  46.                 onClicked: colorMaker.colorMade(colorMaker.color)  
  47.             }  
  48.         }  
  49.     }  
  50.   
  51.     Loader {  
  52.         id: redLoader  
  53.         anchors.left: rect1.right  
  54.         anchors.leftMargin: 50  
  55.         sourceComponent: colorComponent  
  56.         onLoaded: item.color = "red"  
  57.     }  
  58.     Loader {  
  59.         id: greenLoader  
  60.         anchors.right: rect2.left  
  61.         anchors.rightMargin: 50  
  62.         sourceComponent: colorComponent  
  63.         onLoaded: item.color = "green"  
  64.     }  
  65.   
  66.     Connections {  
  67.         target: redLoader.item  
  68.         onColorMade: {  
  69.             rect2.changeColor(text2)  
  70.             text1.color = col  
  71.         }  
  72.     }  
  73.   
  74.     Connections {  
  75.         target: greenLoader.item  
  76.         onColorMade: {  
  77.             rect2.changeColor(text2)  
  78.             text1.color = col  
  79.         }  
  80.     }  
  81. }  

这段代码综合了许多知识点,貌似有点复杂,我们且看且分析。在rect2中自定义了一个函数changeColor(obj),目的是随机改变obj的颜色,函数参数没有显式地指明其类型,是个动态类型。Component定义了一个组件,这里我们自定义的信号是colorMade(color col),信号的参数类型是要明确指明的,信号处理器可以使用这个参数,在C++中使用emit关键字发送一个信号,在QML中信号就是个特殊的函数,可以像函数那样来使用。Loader动态加载一个组件,其item属性保存的就是加载的那个组件,然后用Connections来连接我们自定义的信号,在信号处理器中又调用了我们自定义的函数。

6、使用connect() / disconnect()

再介绍一个连接信号的方法,信号本身有connect() / disconnect()两个函数来连接或断开一个信号或方法,不多说了,直接看代码:

[plain] view plain copy
  1. import QtQuick 2.3   
  2.   
  3. Item {  
  4.     id: item  
  5.   
  6.     signal employer(string name)  
  7.     signal employee(string name)  
  8.   
  9.     onEmployee: console.log(name,“from signal”)  
  10.   
  11.     function employ(name) {  
  12.         console.log(name,“from function”)  
  13.     }  
  14.   
  15.     Component.onCompleted: {  
  16.         item.employer.connect(employ)  
  17.         item.employer.connect(employee)  
  18.         employer("Bible")  
  19.     }  
  20. }  

例子中用信号分别连接了另外一个信号和函数,比较简单,不再介绍了。


转载:http://blog.csdn.net/iEearth/article/details/41801333

0 0