第六章:QML信号

1、QML属性变化信号

on<Property>Changed

示例:

import QtQuick 2.0

TextInput {
    text: "Change this!"
    onTextChanged: {//text属性信号处理
        console.log("Text has changed to:", text)
    }
    MouseArea{
        anchors.fill:parent
        onClicked: {
           textChanged()//调用text属性信号         
        }
   }
}

2、QML自定义信号

signal <name>[([<type> <parameter name>[, ...]])]

示例:

import QtQuick 2.1

Rectangle {
    id: rectangle
    color: "red"
    width: 400
    height: 200
    // 发送给 Qt Widgets 的信号
    signal qmlSignal
    // 从 Qt Widgets 接收到的信号
    signal cSignal

    Text {
        id: text
        text: "This is QML code"
        font.pointSize: 14
        anchors.centerIn: parent
        PropertyAnimation {    //属性动画
            id: animation
            target: text
            property: "rotation"
            from: 0; to: 360; duration: 5000
            loops: Animation.Infinite
        }
    }
    MouseArea {
        anchors.fill: parent
        onClicked: qmlSignal()
    }
    // 信号处理程序(处理从 Qt Widgets 接收到的信号)
    onCSignal: {
        animation.paused ? animation.resume() : animation.pause()
    }
    Component.onCompleted: animation.start()
}

3、处理任意对象的信号

QtQuick 框架提供了一个类型Connections ,用于处理任意对象的信号

Connections介绍: https://doc.qt.io/qt-5/qml-qtqml-connections.html

示例:

Connections {
        target: theworker  //指向发出信号的对象
        onStarted: {
            print('线程开启')
        }
        onDataChanged: {
            print('计时改变: ' + theworker.getData() );
            thetext.text = theworker.getData();
        }
    }

参考: https://blog.csdn.net/douzhq/article/details/81007504

猜你喜欢

转载自blog.csdn.net/qq_40602000/article/details/109277460