QML 信号与槽 方式1 对于 QML 中的属性如果其值发生改变, QML 自动会发生相关信号
on<Property>Changed 这种格式
举例:
1 2 3 MouseArea { onPressedChanged: console .log("value:" , pressed) }
方式2 比较适合在同一个 QML 文件内
1 2 3 signal <name> (type parameter, type parameter) on<Name>
例如:
1 2 3 4 signal testSignal(real x, real b) testSignal(x, b) //执行 也就是 发送信号 类似 quick 中的 emit signal() onTestSignal: console.log("xxx" )// 槽 用于接收信号
举例:
1 2 3 4 5 6 7 8 9 10 11 Item { signal clickTest(); MouseArea { onPressed: { clickTest() } } onClickTest: consloe.log("received") }
方式3 适合一对多或者跨 QML 断开就使用 disconnect 就好 1 : 跟信号在同一个范围,可这么写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 signal sendSignal(); MouseArea { sendSignal() } Component.onCompleted: { sendSignal.connect(send21) sendSignal.connect(send22) sendSignal.connect(send23) } function send21() { console.log("1: received signal" ); } function send22() { console.log("2: received signal" ); } function send23() { console.log("3: received signal" ); }
2:如果与信号不在同一范围
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 MyTest { signal testT() id : mytest MouseArea { onPressed: { mytest.testT() } } } Component.onCompleted: { mytest.testT.connect(send21) mytest.testT.connect(send22) mytest.testT.connect(send23) } function send21 ( ) { console .log("1: received signal" ); } function send22 ( ) { console .log("2: received signal" ); } function send23 ( ) { console .log("3: received signal" ); }
3:Connections 最主要的优势可以连接到没有定义在 QML 的东西 格式:
1 2 3 4 Connections { target: 信号的来源 on<Signal>: }
示例1 2 3 4 5 6 Connections { target: mytest onTestT: { send21(); } }
QML中调用Python的函数 main.qml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import QtQuick 2.15 import QtQuick.Controls 2.15 import QtQuick.Window 2.15 Window { width: 600 ; height: 400 visible: true title: "主页面" Button { text: "打印" onClicked: { pageData.mlog("你好" ) } } }
main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 import osfrom pathlib import Pathimport sysfrom PySide2.QtCore import *from PySide2.QtGui import QGuiApplicationfrom PySide2.QtQml import QQmlApplicationEnginefrom PySide2.QtWidgets import QApplicationclass MainWin (QObject) : @Slot(str) def mlog (self, s) : print(s) if __name__ == '__main__' : app = QApplication() engine = QQmlApplicationEngine() pd = MainWin() engine.rootContext().setContextProperty('pageData' , pd) engine.load(QUrl.fromLocalFile('./main.qml' )) sys.exit(app.exec_())
Python 连接 QML 信号 (QML 发信号, Python 为槽)
main.qml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import QtQuick 2.15 import QtQuick.Controls 2.15 import QtQuick.Window 2.15 Window { visible: true width: 600 ; height: 400 signal mlog(string s) Button { text: "hello" onClicked: mlog(text) } }
main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import sysfrom PySide2.QtCore import *from PySide2.QtQml import QQmlApplicationEnginefrom PySide2.QtWidgets import QApplicationdef log (s) : print(s) if __name__ == '__main__' : app = QApplication() engine = QQmlApplicationEngine() engine.load(QUrl.fromLocalFile('./main.qml' )) root = engine.rootObjects()[0 ] target_view = root target_view.mlog.connect(log) sys.exit(app.exec_())
Python获取控件 1 2 3 4 5 6 7 Text { id: mytext objectName: "mytext" text: "Click Me" font.pixelSize: 20 anchors.centerIn: parent }
获取控件
1 txt_obj = engine.rootObjects()[0 ].findChild(QObject, "mytext" )
获取控件属性的值:
1 txt_value = txt_obj.property("text" )
设置控件属性的值:
1 txt_obj.setProperty("text" , "Clicked!" )