lately I have been reworking a program that I wrote a while ago. Basically a simple python script in the backend with PySide6 and QtWidgets as my frontend. Since I am working a lot with Qml and declarative Gui design at work I wanted to rebuild my QtWidgets Gui and use Qml instead. Yet it is working perfectly fine except for the fact that I have no clue how to connect my backend/python functionality into my Qml Gui.
This is my python code:
from provisioning_tool import execute_pvt
import qrc
import sys
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine, QmlElement
from PySide6.QtCore import (
QObject,
Slot,
Signal)
if __name__ == "__main__":
app = QGuiApplication()
engine = QQmlApplicationEngine()
engine.load('QML/application.qml')
sys.exit(app.exec())
This is an extract from my .qml file:
ApplicationWindow {
id: root
height: 600
minimumHeight: 600
minimumWidth: 800
title: qsTr("Provisioning Tool")
visible: true
width: 800
...
Button{
id: button
onClicked: execute Python function???
}
...
}
The script in question is the imported provisioningtool and I wanna use the execute_pvt function from it. During my research I found many different approaches including setting properties using engine.rootObjects.setProperty()... or using Bridge classes and declaring them as Qml elements but nothing seemed to work for me. Any suggestions on how to tackle this?
Many thanks in advance
According to suggestions and as I already did before I reworked my code according to an official PySide documentation tutorial: https://doc.qt.io/qtforpython-6.2/tutorials/qmlintegration/qmlintegration.html
main.py
from provisioning_tool import execute_pvt
import qrc
import sys
from PySide6.QtCore import (
QObject,
Slot,
Signal)
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine, QmlElement
QML_IMPORT_NAME = "io.qt.textproperties"
QML_IMPORT_MAJOR_VERSION = 1
if __name__ == "__main__":
@QmlElement
class Bridge(QObject):
@Slot(str)
def writeConsole():
print("It works!")
app = QGuiApplication()
engine = QQmlApplicationEngine()
engine.load('QML/application.qml')
sys.exit(app.exec())
application.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Controls.Material 2.15
import QtQuick.Dialogs as Dlg
import QtQuick.Layouts 2.15
import QtQuick.Window 2.2
ApplicationWindow {
id: root
height: 600
minimumHeight: 600
minimumWidth: 800
title: qsTr("Provisioning Tool")
visible: true
width: 800
Bridge {
id: bridge
}
...
Button{
onClicked: bridge.writeConsole()
}
...
}
However, whene executing the code I get the following message: file:///C:/Users/pkamp/provisioningtool/QML/application.qml:18:5: Bridge is not a type.
What is the problem here?