在qt quick中信号处理同样离不开信号和槽,在qt quick中处理信号主要用到的是信号处理器,附加信号处理器,Connections来处理信号的。
1.首先介绍下信号处理器
Rectangle{
Button{
…
onClicked:{ }
…
}
}
在信号onClicked后面跟了一个匿名函数,来处理信号的。
2.附加信号处理器
Item{
…
…
keys.onReturnPressed: console.log(“1111”);
}
其中的onReturnPresssed其实是我们为keys对象的retuenPressed信号准备的附加信号处理器。
在前面介绍的两种方面,在有些情况下会不方便,在这个时候就会用到Connections来处理信号了。
import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
import QtQuick.Dialogs 1.1
Rectangle{
width: 320;
height: 240;
color: "gray";
Text {
id: text1
text: qsTr("Text One");
anchors.horizontalCenter: parent.horizontalCenter;
anchors.top: parent.top;
color:"blue";
font.pixelSize: 28;
}
Text{
id:text2;
anchors.horizontalCenter: parent.horizontalCenter;
anchors.top: text1.bottom;
anchors.topMargin: 8;
text:"Text Two";
color: "blue";
font.pixelSize: 28;
}
Button{
id:changeButton;
anchors.top: text2.bottom;
anchors.topMargin: 8;
anchors.horizontalCenter: parent.horizontalCenter;
text: "Change";
}
Connections{
target: changeButton;
onClicked:{
text1.color = Qt.rgba(Math.random() , Math.random() , Math.random(),1);
text2.color = Qt.rgba(Math.random() , Math.random() , Math.random(),1);
}
}
}
关于qt quick信号的使用差不多就是这些了吧