This article describes the process using a function QML Python parsing, text sample code described in great detail, has a certain reference value of learning for all of us to learn or work, a friend in need under reference
there are two ways:
A, QML define a signal, Python in connection function;
Function here do not specifically designated as slot function, normal function can be.
QML Python function signal connections of
QML:
first defined in QML a signal, where the signal passed to the function of a string (with parameter signals may also or without):
signal mySignal(string my_string)
then transmits this signal to the click of:
onClicked:{
root.mySignal("hello world")
}
Python:
Use in QML signal connection function in Python:
engine.rootObjects()[0].mySignal.connect(my_func) # 这里的mySignal是在QML里定义的
complete code:
QML:
import QtQuick 2.12
import QtQuick.Controls 2.12
ApplicationWindow {
id: root
width: 250
height: 500
visible: true
signal mySignal(string my_string)
MouseArea {
id: mouse_area
anchors.fill: parent
onClicked: {
root.mySignal("hello world")
}
}
}
Python:
from PyQt5.QtCore import QObject
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
import sys
class MyWindow(QObject):
def __init__(self):
super().__init__()
self.engine = QQmlApplicationEngine()
self.engine.load('qml-test.qml')
# root signal
my_obj = self.engine.rootObjects()[0]
my_obj.mySignal.connect(self.my_func)
def my_func(self, my_string):
print(my_string)
if __name__ == '__main__':
app = QGuiApplication(sys.argv)
window = MyWindow()
sys.exit(app.exec())
Two, define a Python class and slot function, use this function in the slot in QML
call Python function in the slot in QML
first need to define a class in Python, write a groove function in class:
class Person(QObject):
def __init__(self):
super().__init__()
@pyqtSlot() # 注意是槽函数!
def begin(self):
print('begin')
SetContextProperty this class then a set of context attribute values:
person = Person()
engine.rootContext().setContextProperty('person', person)
QML file without any special settings, you can directly call the function.
Complete code:
Python:
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtCore import QObject, pyqtSlot
import sys
class Person(QObject):
def __init__(self):
super().__init__()
@pyqtSlot() # 注意是槽函数!
def begin(self):
print('begin')
if __name__ == '__main__':
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
person = Person()
engine.rootContext().setContextProperty('person', person)
engine.load('qml-test.qml')
sys.exit(app.exec())
QML:
import QtQuick 2.12
import QtQuick.Controls 2.12
ApplicationWindow {
id: root
width: 250
height: 500
visible: true
Button{
text:qsTr("begin")
onClicked: {
person.begin()
}
}
}
We recommend the python learning sites , to see how old the program is to learn! From basic python script, reptiles, django, data mining, programming techniques, work experience, as well as senior careful study of small python partners to combat finishing zero-based information projects! Every day, Python programmers explain the timing of technology, sharing some learning methods and the need to pay attention to small details,