Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved QQMLApplicationEngine failed to load component; Number generator is not a type

    Qt for Python
    python qt for python pyside
    2
    2
    1078
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      SCP173 last edited by SCP173

      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?

      eyllanesc 1 Reply Last reply Reply Quote 0
      • eyllanesc
        eyllanesc @SCP173 last edited by

        @SCP173

        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
                }
            }
        }
        

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        1 Reply Last reply Reply Quote 0
        • First post
          Last post