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

39 lines
982 B
Python
Raw Normal View History

2018-04-16 23:17:02 +08:00
#!/usr/bin/env python
'''
1QML显式的调用Python函数
定义一个类并继承QtCore.QObject对象并使用@修饰符修饰pyqtSlot
创建rootContext对象并使用setContextPropertystring, object注册对象
这样在QML中就可以调用这个函数了
这个例子运行后如果点击鼠标的话会在控制台打印字符串
'''
from PyQt5.QtCore import QUrl, QObject, pyqtSlot
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQuick import QQuickView
class MyClass(QObject):
@pyqtSlot(str) # 输入参数为str类型
def outputString(self, string):
print(string)
if __name__ == '__main__':
app = QGuiApplication([])
path = 'test.qml' # 加载的QML文件
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_()