And a function of a signal in QML
https://blog.csdn.net/iEearth/article/details/41801333
In Qt C ++ in, signals & slots is the core mechanism is the application components that interact with the UI neural networks, also in QML, there are similar signal & handler mechanism, signal signal is, we often say that an event by signal handler signal processor to process the signal. When a signal is transmitted signals, signal processors corresponding handler is triggered, we can do something at handler signal processor to process the signal signal event.
1, "HelloWorld" Brief signal
The following is a simple example:
import QtQuick 2.3
import QtQuick.Controls 1.2
Rectangle {
width: 360; height: 200
color: "lightblue"
Button {
text: "Quit!"
anchors.centerIn: parent
onClicked: Qt.quit()
}
}
There is a Button in a Rectangle, this has a default Button clicked () signal, there will be a corresponding signal processor onClicked. If there is a signal () signal, then the signal processor corresponding handler is onSignal, note that case, the signal has a signal processor receives the parameter and can be used. When we click the Button example of the left mouse button, will be sent clicked () signal, the trigger signal processor onClicked, here called Qt.quit () function to exit the application.
2, attribute handler
When the attribute value change QML, will automatically send a signal to the attribute value change corresponding to the format of the signal processor is on <Property> Changed, whether built-in QML properties, or our custom attribute, attribute value once change will trigger a corresponding signal processor, for example as follows:
import QtQuick 2.3
import QtQuick.Controls 1.2
Rectangle {
width: 360; height: 100
color: "lightblue"
onColorChanged: console.log("color changed")
Button {
text: "Change Color"
anchors.centerIn: parent
onClicked: parent.color = "yellow"
}
}
When you click the button Button, Rectangle's color from the original lightblue will become yellow, then it will trigger onColorChanged, output "color changed".
3, additional signalhandler
Additional signal processor receives a signal from the additional element type, rather than the current object, for example in the following examples and Keys Component:
import QtQuick 2.3 Rectangle { id: rect width: 360; height: 100 focus: true Keys.onSpacePressed: rect.color = Qt.rgba(Math.random(), Math.random(), Math.random(), 1) Component.onCompleted: rect.color = "green" }
We use the example described above Component.onCompleted, set the color component is completed when the Rectangle is green, also used Keys.onSpacePressed, Rectangle random changes color when pressing the spacebar. Math is a JavaScript object, where we can directly be used because QML is an extension of JavaScript, JavaScript support of most things.
4, using the connection handler Connections
In the previous example, we directly use a signal in the signal of the current object on <Signal> in the form of a signal processor, but in some cases we have to use to connect a signal Connections, for example, when a plurality of objects connected to the same signal, establish a connection, the connection is not defined in QML object (such as object defined in C ++) objects outside the scope of the transmitted signal, illustrated:
import QtQuick 2.3 Item { width: 500; height: 150 Row { spacing: 50 Rectangle { width: 100; height:100 color: "red" MouseArea { anchors.fill: parent onClicked: parent.color= Qt.rgba(Math.random(), Math.random(), Math.random(), 1) } } Rectangle { id: rectYellow width: 100; height:100 color: "yellow" MouseArea { anchors.fill: parent Connections { onClicked: rectYellow.color= Qt.rgba(Math.random(), Math.random(), Math.random(), 1) } } } Rectangle { id: rectGreen width: 100; height:100 color: "green" MouseArea { id: mouseArea anchors.fill: parent } Connections { target: mouseArea onClicked: rectGreen.color= Qt.rgba(Math.random(), Math.random(), Math.random(), 1) } } } }
The above example, include three usage. The first, similar to the second use, are directly connected in MouseArea scope, it can be seen Connections A second method may be omitted, MouseArea specifies a region to handle the mouse event, in the third method is the scope MouseArea establish a connection outside, it had to use connections, three methods are random changes color when a mouse click Rectangle.
5, the custom signals, function
In QML, we can customize the signals and functions, you first need to declare that, not as support overloading like C ++, the syntax is as follows:
signal <signalName>[([<type> <parameter name>[, …]])] function <functionName>([<parameterName>[, ...]]) { <body> }
Since the signal and function definitions can take parameters, the difference is that the signal to indicate the parameter type, and the function they do not need, which is why, because the parameter type of the function defaults to the Almighty var type, but did not like C ++ as must specify the return value type, the following is an example of the integrated:
import QtQuick 2.3 Item { width: 600; height: 100 Rectangle { id: rect1 width: 100; height: 100 color: "lightblue" Text { id: text1 anchors.centerIn: parent text: "custom \n signal" } } Rectangle { id: rect2 width: 100; height: 100 anchors.right: parent.right color: "lightblue" function changeColor(obj) { obj.color = Qt.rgba(Math.random(), Math.random(), Math.random(), 1) } Text { ID: text2 anchors.centerIn: parent text: "Custom \ n-funtion" } } the Component { ID: colorComponent the Rectangle { ID: colorMaker width: 100; height: 100 Signal colorMade (Color COL) // signal the MouseArea { anchors.fill: parent the onClicked : colorMaker.colorMade (colorMaker.color) // call the function signal, the transmitted signal similarly colorMade } } } Loader { ID: redLoader anchors.left: rect1.right anchors.leftMargin: 50 sourceComponent: colorComponent the onLoaded: item.color = "Red" } Loader { ID: greenLoader anchors.right: rect2.left anchors.rightMargin: 50 sourceComponent: colorComponent the onLoaded: item.color = "Green" } the Connections { // the audiences connecting function signals and slots target: redLoader.item onColorMade: { // function processing tank audiences signal rect2.changeColor (text2) // text1.color COL = } } the connections { // the target object signals and slots function connection target: greenLoader.item onColorMade: {// slot function processing target object signal rect2.changeColor (text2) text1.color COL = } } }
This code combines a lot of knowledge points, looks like a little complicated, and we shall see and analyze. In rect2 a custom function changeColor (obj), the color of object obj is randomly changed, the function parameters are not explicitly indicate its type, is a dynamic type. Component definition of a component, where our custom signal is colorMade (color col), parameter-type signal is to be clearly identified, the signal processor can use this parameter to send a signal using the emit keyword in C ++, in QML signal is a special function that can be used like functions. Loader component dynamically load a saved item attribute is that its components are loaded, and then use Connections to connect our custom signal, the signal processor and call our custom function.
6, using the connect () / disconnect ()
Further describes a method of connecting signal, the signal itself connect () / disconnect () function to connect or disconnect the two signals or a method, not much to say, a direct look at the code:
2.3 QtQuick Import Item { ID: Item Signal Employer (String name) Signal employee (String name) // employee of the slot function onEmployee: Console .log (name, "Signal from") function employ (name) { the console.log (name , "from function") } Component.onCompleted: { // connecting a plurality of grooves functions Employer signal or signals . item.employer connect (employ) . item.employer connect (Employee) Employer ( "Bible") } }
Examples are connected by signal and another signal function, relatively simple, not introduced.
============== End