TypeError: Property 'printText' of object AppCore(0x268510a3e60) is not a function

Viewed 23

I'm trying to put the core object of the CoreApp class into a QML file with context.setContextProperty("appCore", core), but it does not work correctly

Code:

main.py:

import os
from pathlib import Path
import sys

from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine

from AppCore import AppCore


if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    context = engine.rootContext()
    core = AppCore()
    context.setContextProperty("appCore", core)
    engine.load(os.fspath(Path(__file__).resolve().parent / "main.qml"))
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec())

AppCore.py:

from PySide6 import QtCore


class AppCore(QtCore.QObject):

    def __init__(self, parent=None):
        super().__init__(parent)

    def printText(self, text: str):
        print(text)

a part of the main.qml:

XButton {
    id: b1
    text: "Button"
    onClick: {
        appCore.printText("qwe")
    }
}

So, I get the following output:
file:/ .. main.qml: TypeError: Property 'printText' of object AppCore(0x2a5f5ee7520) is not a function
I get some information about the object in qml, but there is nothing inside.
If it helps, console.log(JSON.stringify(appCore)) returns {"objectName":""}

0 Answers
Related