Using Python class files in QML

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I have a main.qml file that look like below

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5


ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    TextField {
        id:textarea
        anchors.centerIn: parent
        Button {
            text: "Click Me"
            anchors.leftMargin: 34
            id:textareabutton
            y: 0
            anchors.left:textarea.right
            onClicked: {
                someclass.say(textarea.text)
            }
        }
    }
    TextField {
        id:textarea2
        anchors.horizontalCenterOffset: 0
        anchors.topMargin: 37
        anchors.top: textarea.bottom
        anchors.horizontalCenter: textarea.horizontalCenter
    }
    Connections {
        target: someclass

        onToPython : {
            textarea2.text = say
        }
    }
}

i have a python class file which i have added using add file option in qtcreator, when i run main.qml i am getting errors related to the classes being undefined as below

qrc:/main.qml:33:5: QML Connections: Cannot assign to non-existent property "onToPython"
qrc:/main.qml:34: ReferenceError: someclass is not defined
qrc:/main.qml:22: ReferenceError: someclass is not defined

I have the external tools configured in the Qt creator for python and when i run through it,it works. However it is not working when i run main.qml. What am i missing, how can i make use of the python class file.

below is the python file that invokes the QML , if i run from python it works, i want to run qml file and invoke this class

import sys
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtCore import QObject,pyqtSignal,pyqtSlot

class someclassr(QObject):
    def __init__(self):
        QObject.__init__(self)

    toPython=pyqtSignal(str, arguments
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

Short answer: There's no built-in integration between QML and Python. I'm not sure why one'd assume that there was, but there really isn't. Qt Creator is a multi-language IDE, and its support for Python doesn't imply that QML and Python are integrated.

Having said that, Python classes can be easily integrated with Qt and QML using PyQt. If you don't want to depend on PyQt, you can integrate the two manually by writing adapter classes that call into the Python runtime that your application would link with.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.