Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] About showing QWindow
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] About showing QWindow

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 3.2k Views 1 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.
  • M Offline
    M Offline
    madoodia
    wrote on last edited by
    #1

    hi guys
    i am working on a project that use PyQt5 and QML.
    i have this two files and they work:
    PyQt file:
    @import sys
    from PyQt5.QtCore import QUrl
    from PyQt5.QtWidgets import QApplication
    from PyQt5.QtQuick import QQuickView

    app = QApplication(sys.argv)

    view = QQuickView()
    view.setSource(QUrl("example.qml"))
    view.show()

    sys.exit(app.exec_())@

    and QML File:

    @import QtQuick 2.0

    Item {
    id: root
    width: 300; height: 100

    Rectangle {
    id: bg
    anchors.fill: parent
    color: "red"

     Text {
         id: helloworld
         text: "Hello world!"
         anchors.centerIn: parent
         color: "White"
         font.pointSize: 20
     }
    

    }
    }@

    this project work.
    but question here:
    if instead of Item type that is instanced from QQuickItem, i use ApplicationWindow that instanced from QWindow, QQuickView can not show the window for me.
    i want to know that what Class of QtQuick should i use to show ApplicationWindow from PyQt?

    PyQt File: (same as before)
    @import sys
    from PyQt5.QtCore import QUrl
    from PyQt5.QtWidgets import QApplication
    from PyQt5.QtQuick import QQuickView

    app = QApplication(sys.argv)

    view = QQuickView()
    view.setSource(QUrl("example.qml"))
    view.show()

    sys.exit(app.exec_())@

    QML File:

    @import QtQuick 2.0
    import QtQuick.Controls 1.0

    ApplicationWindow {
    id: root
    width: 300; height: 100
    title: "my window"

    Rectangle {
    id: bg
    anchors.fill: parent
    color: "red"

     Text {
         id: helloworld
         text: "Hello world!"
         anchors.centerIn: parent
         color: "White"
         font.pointSize: 20
     }
    

    }
    }@

    the error is : QQuickView only supports loading of root objects that derive from QQuickItem.

    thanks in advance

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xander84
      wrote on last edited by
      #2

      Hi, if you want to use an QML ApplicationWindow as the root element you can't use QQuickView. I don't know if there is any official example for it but I can show you I use it in my app, this is c++ though, but I think you can easily convert it to Python:
      @
      QQmlApplicationEngine engine(QUrl("qrc:///main.qml"));
      QObject *topLevel = engine.rootObjects().value(0);
      QQuickWindow window = qobject_cast<QQuickWindow>(topLevel);
      window->show();
      @
      so you have to create the QQmlApplicationEngine yourself and just pass it your QML file containing the ApplicationWindow.
      That is at least how I use it. :)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        madoodia
        wrote on last edited by
        #3

        Thanks
        i try to change it to python
        but i don't know how change this line to it:
        @QQuickWindow window = qobject_cast<QQuickWindow>(topLevel);@

        because python don't have pointer..

        1 Reply Last reply
        0
        • X Offline
          X Offline
          Xander84
          wrote on last edited by
          #4

          I have no experience with Python, but since it is a scripting language I guess you can just ignore that part and call the "show" function, try this:
          @
          engine = QQmlApplicationEngine(QUrl("qrc:///main.qml"))
          window = engine.rootObjects().value(0)
          window.show()
          @
          ( this is my first "phython" script so I have no idea if that is valid code, just used your code as example haha)

          1 Reply Last reply
          0
          • M Offline
            M Offline
            madoodia
            wrote on last edited by
            #5

            oh , yes.
            i don't know that line but as you said i ignore it and this code works in PyQt:
            @import sys
            from PyQt5.QtCore import QUrl
            from PyQt5.QtGui import QGuiApplication
            from PyQt5.QtQml import QQmlApplicationEngine

            app = QGuiApplication(sys.argv)

            engine = QQmlApplicationEngine(QUrl("example.qml"))
            window = engine.rootObjects()[0]
            window.show()

            sys.exit(app.exec_())@

            really thanks dear Xandar

            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