QML笔记-对QML中信号与槽的进一步认识
生活随笔
收集整理的這篇文章主要介紹了
QML笔记-对QML中信号与槽的进一步认识
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
?
?
基本概念
代碼及實(shí)例
?
基本概念
通常情況下,使用自定義控件(多個(gè)形狀進(jìn)行組合),要使用信號(hào)與槽進(jìn)行關(guān)聯(lián),一般出現(xiàn)這種情況,都會(huì)采用JavaScript代碼進(jìn)關(guān)聯(lián)。
?
?
代碼及實(shí)例
如下這個(gè)例子,Notifier和Receiver實(shí)現(xiàn)了關(guān)聯(lián)。
運(yùn)行截圖如下:
當(dāng)數(shù)據(jù)增加后:
點(diǎn)擊左邊,右邊也會(huì)跟著變。
關(guān)鍵就是利用到了這個(gè):
Component.onCompleted: {notifierId.notify.connect(receiverId.receiveInfo)}程序結(jié)構(gòu)如下:
源碼如下:
main.qml
import QtQuick 2.9 import QtQuick.Window 2.2Window {visible: truewidth: 640height: 480title: qsTr("Hello World")Notifier{id: notifierIdrectColor: "yellowgreen"}Receiver{id: receiverIdrectColor: "dodgerblue"anchors.right: parent.right}Component.onCompleted: {notifierId.notify.connect(receiverId.receiveInfo)} }Notifier.qml
import QtQuick 2.0Item {property alias rectColor: notifierRectId.colorwidth: notifierRectId.widthheight: notifierRectId.heightproperty int count: 0signal notify(string count)property Receiver target: nullRectangle{id: notifierRectIdwidth: 200height: 200color: "red"Text {id: displayTextIdanchors.centerIn: parentfont.pointSize: 20text: count}MouseArea{anchors.fill: parentonClicked: {count++notify(count)}}} }Receiver.qml
import QtQuick 2.0Item {property alias rectColor: receiverRectId.colorwidth: receiverRectId.widthheight: receiverRectId.heightfunction receiveInfo(count){receiverDisplayTextId.text = countconsole.log("Receiver received number : " + count)}Rectangle{id: receiverRectIdwidth: 200height: 200color: "red"Text {id: receiverDisplayTextIdanchors.centerIn: parentfont.pointSize: 20text: "0"}} }?
總結(jié)
以上是生活随笔為你收集整理的QML笔记-对QML中信号与槽的进一步认识的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Docker的基本认识及使用
- 下一篇: Python笔记-requests获取w