Qt Quick中的信号机制

在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信号的使用差不多就是这些了吧

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值