Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Basic QQmlApplicationEngine in PySide6



  • I'm unable to get a QML ui running using QQmlApplicationEngine. The program exits -1 because there is nothing in engine.rootObjects().

    I'm new to Qt, any help would be much appreciated

    import sys
    
    from pathlib import Path
    from PySide6.QtCore import QUrl
    from PySide6.QtGui import Qt, QGuiApplication
    from PySide6.QtQml import QQmlApplicationEngine
    
    
    def main():
    
        qml = str(Path(__file__).parent.joinpath('main.qml'))
        app = QGuiApplication(sys.argv)
        app.setAttribute(Qt.AA_EnableHighDpiScaling)
    
        engine = QQmlApplicationEngine(parent=app)
    
        engine.load(QUrl.fromLocalFile(qml))
    
        if not engine.rootObjects():
            sys.exit(-1)
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    

    QML file

    import QtQuick 2.12
    import QtQuick.Controls 2.12
    
    ApplicationWindow {
        id: root
        visible: true
        title: qsTr("My Title")
    
        contentItem {
            minimumWidth: 800
            minimumHeight: 600
        }
    
        menuBar: MenuBar {}
    
        toolBar: ToolBar {}
    
        statusBar: StatusBar {}
    }
    


  • @sunkeita change to

    import QtQuick
    import QtQuick.Controls
    
    ApplicationWindow {
        id: root
        visible: true
        title: qsTr("My Title")
        minimumWidth: 800
        minimumHeight: 600
        menuBar: MenuBar {}
    }
    

    Note: in Qt6 it is not to indicate the versions of the QML libraries. On the other hand ApplicationWindow does not have the toolBar and statusBar properties, read https://doc-snapshots.qt.io/qt6-dev/qml-qtquick-controls2-applicationwindow.html



  • @eyllanesc Thanks for the quick response, however this doesn't seem to work either. I also tested my original qml on PySide2 and it also doesn't work. I think I'm missing something more fundamental about how to load qml with qqmlapplicationengine.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Might be a silly question but are you sure about your QML file path ?



  • @sunkeita I have tested the code with PySide2 and I get the same errors and the solution was the same.



  • @SGaist
    The QUrl I am passing looks like this:

    PySide6.QtCore.QUrl('file:///C:/Users/alan/PycharmProjects/sozu-tmt/sozu/main.qml')
    

    The URI scheme with a Windows path name looks correct to me. I'm looking at trying to pass this in as a qresource in python which should move path lookup concerns to the point that resources are built.



  • @sunkeita The url is correct, the problem is that you are using properties that do not exist. I recommend you review the docs: https://doc.qt.io/qt-5/qml-qtquick-controls2-applicationwindow.html to know what properties exist.



  • @eyllanesc Ok, that's good to know. I changed toolbar and statusbar to header and footer, and I've left out the minimum width and height for now. I haven't seen any docs about dropping the version from the import; the docs you linked have versioned imports as well. Could you provide a bit more clarity on this one?



  • @eyllanesc I made changes to the properties that I am using, as noted in the other thread. I haven't seen any doc that removes versions from the import line. After reviewing the documentation you linked, this is what I have.

    I haven't gotten an actual error message about the qml load, it just doesn't get loaded. Not sure if that is because of bad qml or a qml path issue although I'm pursuing both as possible issues.

    Thank you for your time and assistance!

    import QtQuick 2.12
    import QtQuick.Controls 2.12
    
    ApplicationWindow {
        id: root
        visible: true
        title: qsTr("MyTitle")
    
        menuBar: MenuBar {}
    
        header: ToolBar {}
    
        footer: TabBar {}
    
        StackView {
            anchors.fill: parent
        }
    }
    


  • @sunkeita 1) You have not received any error message because as soon as your qml is launched, the interpreter cannot understand it so no rootObjects is built and therefore sys.exit(-1) is executed, 2) I recommend you check the blog of Qt since there they point out the new modifications that come with Qt6 that are not yet documented, unfortunately, at this moment I cannot find the link but when I find it I will give it to you but you can read this: https://jbbgameich.github.io/qt/2020/05/18/compiled-qml.html 3) If you want to get the warnings then use qmlscene: qmlscene.exe main.qml



  • @sunkeita See https://wiki.qt.io/New_Features_in_Qt_6.0 in seccion QML:

    Qt QML

    • The QML import and versioning system has been overhauled:
      • Version numbers are optional in the QML language. An import without any version number imports the latest version of the module. An import with only a major version number imports the module with specified major version and the latest minor version.
      • QML supports "auto" imports: Instead of specifying a version number, one can write import <module> auto. In that case the imported module is imported in the same version as the importing module.


  • @eyllanesc Thanks so much! On a lark I decided to try running this on my linux machine, and it worked just fine. I will try using qmlscene on my windows machine to find the errors there. I opened this thread to ensure that I was using QQmlApplicationEngine correctly, this issue is resolved.

    Thanks again :)


Log in to reply