QML using Python function

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.

Python is connected to the signal function QML

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 connections in Python functions:

engine.rootObjects () [0]. mysignal .connect (my_func) # mysignal here is defined in the QML

The 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, Python and the groove define a class function, use this function in QML groove.

Python calls the function in the slot in QML

First you need to define a class in Python, write a groove function in class:

 

 class the Person (QObject):
     DEF  __init__ (Self): 
        Super (). __init__ () @pyqtSlot ()   

    # note the slot function! 
    DEF the begin (Self):
         Print ( ' the 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()
            }
    }
}

 

-- END --

 

Guess you like

Origin www.cnblogs.com/ibgo/p/11589613.html