PyQt/partner_625781186/QML_QtQuick_PY/python_QML调用基础/3-QML连接信号到Python/qml-test3.py
2018-04-16 23:17:02 +08:00

31 lines
900 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
'''
3QML连接信号到Python
当QML触发事件的时候发射一个信号给Python此时Python调用一个函数。
先在QML中定义一个信号
然后在捕获事件的时候,发射信号,
最后Python中创建一个rootObject对象然后连接这个对象
这个例子中,当点击鼠标的时候,控制台会打印信息。
'''
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQuick import QQuickView
def outputString(string):
print(string)
if __name__ == '__main__':
path = 'test3.qml' # 加载的QML文件
app = QGuiApplication([])
view = QQuickView()
view.engine().quit.connect(app.quit)
view.setSource(QUrl(path))
view.show()
context = view.rootObject()
context.sendClicked.connect(outputString) # 连接QML信号sendCLicked
app.exec_()