PyQt/partner_625781186/QML_QtQuick_PY/python_QML调用基础/2-QML显式的调用Python函数,有返回值/qml-test2.py

34 lines
893 B
Python
Raw Normal View History

2018-04-16 23:17:02 +08:00
#!/usr/bin/env python
'''
2QML显式的调用Python函数并有返回
这个例子跟上一个相类似只是这次调用Python的函数具有返回值功能
运行程序后点击鼠标左上角会显示数字30
'''
from PyQt5.QtCore import QUrl, QObject, pyqtSlot
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQuick import QQuickView
class MyClass(QObject):
@pyqtSlot(int, result=str) # 声明为槽输入参数为int类型返回值为str类型
def returnValue(self, value):
return str(value+10)
if __name__ == '__main__':
path = 'test2.qml' # 加载的QML文件
app = QGuiApplication([])
con = MyClass()
view = QQuickView()
view.engine().quit.connect(app.quit)
view.setSource(QUrl(path))
context = view.rootContext()
context.setContextProperty("con", con)
view.show()
app.exec_()