QQMLApplicationEngine failed to load component; Number generator is not a type
-
I am trying to run an example from QT5 Cadaques book - the full official source code can be found from this link here in this directory: ch20-python-assets\ch20-python\src\class
I get a QQMLApplicationEngine failed to load component; Number generator is not a type error
when running the class.py file. I am using Python 3.8.9 and PySide6 on Windows 10 Pro. Am I missing something obvious? -
I am trying to run an example from QT5 Cadaques book - the full official source code can be found from this link here in this directory: ch20-python-assets\ch20-python\src\class
I get a QQMLApplicationEngine failed to load component; Number generator is not a type error
when running the class.py file. I am using Python 3.8.9 and PySide6 on Windows 10 Pro. Am I missing something obvious?Try with:
import sys import random from PySide6.QtGui import QGuiApplication from PySide6.QtQml import QQmlApplicationEngine, qmlRegisterType from PySide6.QtCore import QUrl from PySide6.QtCore import QObject, Signal, Slot class NumberGenerator(QObject): def __init__(self): QObject.__init__(self) nextNumber = Signal(int) @Slot() def giveNumber(self): self.nextNumber.emit(random.randint(0, 99)) if __name__ == '__main__': app = QGuiApplication(sys.argv) engine = QQmlApplicationEngine() qmlRegisterType(NumberGenerator, 'Generators', 1, 0, 'NumberGenerator') engine.load(QUrl("main.qml")) if not engine.rootObjects(): sys.exit(-1) sys.exit(app.exec())
import QtQuick 2.0 import QtQuick.Window 2.0 import QtQuick.Controls 2.0 import Generators 1.0 Window { id: root width: 640 height: 480 visible: true title: "Hello Python World!" Flow { Button { text: "Give me a number!" onClicked: numberGenerator.giveNumber(); } Label { id: numberLabel text: "no number" } } NumberGenerator { id: numberGenerator // Signal argument names are not propagated from Python to QML, so we need to re-emit the signal signal reNextNumber(int number) Component.onCompleted: numberGenerator.nextNumber.connect(reNextNumber) } Connections { target: numberGenerator function onReNextNumber(number){ numberLabel.text = number } } }