Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Basic QQmlApplicationEngine in PySide6
Qt 6.11 is out! See what's new in the release blog

Basic QQmlApplicationEngine in PySide6

Scheduled Pinned Locked Moved Solved Qt for Python
12 Posts 3 Posters 3.3k Views 2 Watching
  • 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 sunkeita

    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 {}
    }
    
    eyllanescE Offline
    eyllanescE Offline
    eyllanesc
    wrote on last edited by eyllanesc
    #2

    @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

    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
    1
    • S Offline
      S Offline
      sunkeita
      wrote on last edited by
      #3

      @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.

      eyllanescE 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Hi and welcome to devnet,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        S 1 Reply Last reply
        0
        • S sunkeita

          @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.

          eyllanescE Offline
          eyllanescE Offline
          eyllanesc
          wrote on last edited by
          #5

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

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

          S 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi and welcome to devnet,

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

            S Offline
            S Offline
            sunkeita
            wrote on last edited by sunkeita
            #6

            @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.

            eyllanescE 1 Reply Last reply
            0
            • S sunkeita

              @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.

              eyllanescE Offline
              eyllanescE Offline
              eyllanesc
              wrote on last edited by
              #7

              @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.

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

              S 1 Reply Last reply
              0
              • eyllanescE eyllanesc

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

                S Offline
                S Offline
                sunkeita
                wrote on last edited by
                #8

                @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?

                1 Reply Last reply
                0
                • eyllanescE eyllanesc

                  @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.

                  S Offline
                  S Offline
                  sunkeita
                  wrote on last edited by sunkeita
                  #9

                  @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
                      }
                  }
                  
                  eyllanescE 2 Replies Last reply
                  0
                  • S sunkeita

                    @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
                        }
                    }
                    
                    eyllanescE Offline
                    eyllanescE Offline
                    eyllanesc
                    wrote on last edited by eyllanesc
                    #10

                    @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

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

                    S 1 Reply Last reply
                    1
                    • S sunkeita

                      @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
                          }
                      }
                      
                      eyllanescE Offline
                      eyllanescE Offline
                      eyllanesc
                      wrote on last edited by
                      #11

                      @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.

                      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
                      2
                      • eyllanescE eyllanesc

                        @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

                        S Offline
                        S Offline
                        sunkeita
                        wrote on last edited by
                        #12

                        @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 :)

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved