Quantcast
Channel: Active questions tagged qtquick2 - Stack Overflow
Viewing all articles
Browse latest Browse all 107

How do I connect my Python code to my Qml Gui? [duplicate]

$
0
0

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_pvtimport qrcimport sysfrom PySide6.QtGui import QGuiApplicationfrom PySide6.QtQml import QQmlApplicationEngine, QmlElementfrom 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_pvtimport qrcimport sysfrom PySide6.QtCore import (    QObject,    Slot,    Signal)from PySide6.QtGui import QGuiApplicationfrom PySide6.QtQml import QQmlApplicationEngine, QmlElementQML_IMPORT_NAME = "io.qt.textproperties"QML_IMPORT_MAJOR_VERSION = 1if __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.15import QtQuick.Controls 2.15import QtQuick.Controls.Material 2.15import QtQuick.Dialogs as Dlgimport QtQuick.Layouts 2.15import QtQuick.Window 2.2ApplicationWindow {    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?


Viewing all articles
Browse latest Browse all 107

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>