[PyQt] QQmlApplicationEngine and QGuiApplication

Jérôme Godbout jerome at bodycad.com
Thu Feb 25 17:37:17 GMT 2016


No sure what is wrong with yours, but I'm using a QQuickView inside a
QApplication and it work well.

import sys

from PyQt5.QtCore import QSettings, QUrl, QRect, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuick import QQuickView, QQuickWindow

class MyQmlApplication(QApplication):
    _application_name = "UNDEFINED"
    _qquickview = None
    _engine = None
    _settings = None

    def __init__(self, title, args):
        QApplication.__init__(self, args)
        self._qquickview = QQuickView()
        self._qquickview.setTitle(title)
        self._qquickview.setResizeMode(QQuickView.SizeRootObjectToView)
        self._qquickview.setGeometry(QRect(100,100,600,800))
        self._engine = self._qquickview.engine()

    def showAndExec(self, qml_url):
        self._qquickview.setSource(qml_url)
        self._qquickview.show()
        return self.exec_()

app = BodycadQmlApplication("MyAppTitle", sys.argv)
app.showAndExec(QUrl("Main.qml"))


On Thu, Feb 25, 2016 at 10:43 AM, Daniel Krause <madakr at web.de> wrote:

> Hello,
>
> I am new to PyQt and having trouble to include QML-Code into an python
> application.
>
> The QML-File (main.qml) I can build in a project in QtCreator (a Qt Quick
> Application was used to create the project).
>
> import QtQuick 2.5
> import QtQuick.Controls 1.4
> import QtQuick.Dialogs 1.2
> import QtQml.Models 2.1
>
> ApplicationWindow {
>     visible: true
>     width: 640
>     height: 480
>     title: qsTr("Hello World")
>
>     menuBar: MenuBar {
>         Menu {
>             title: qsTr("File")
>             MenuItem {
>                 text: qsTr("&Open")
>                 onTriggered: console.log("Open action triggered");
>             }
>             MenuItem {
>                 text: qsTr("Exit")
>                 onTriggered: Qt.quit();
>             }
>         }
>     }
>
>     MainForm {
>         id: mainForm1
>         anchors.fill: parent
>
>         SplitView {
>             id: splitView1
>             width: 100
>             anchors.left: parent.left
>             anchors.leftMargin: 36
>             anchors.bottom: parent.bottom
>             anchors.bottomMargin: 8
>             anchors.top: parent.top
>             anchors.topMargin: 8
>             orientation: Qt.Vertical
>
>
>             Button {
>                 id: btn_welcome
>                 x: 49
>                 y: 21
>                 text: qsTr("Welcome")
>             }
>
>             Button {
>                 id: btn_database
>                 x: 49
>                 y: 50
>                 text: qsTr("Database")
>             }
>
>             MouseArea {
>                 id: mouseArea1
>                 x: 0
>                 y: 191
>                 width: 100
>                 height: 100
>             }
>         }
>     }
> }
>
> It results in the following window:
> [image: Inline-Bild 1]
>
> To create the application in Python I tried to use QGuiApplication
> and  QQmlApplicationEngine.
> main.py and main.qml are located in the same directory:
>
> import sys
> from PyQt5.QtQml import QQmlApplicationEngine
> from PyQt5.QtGui import QGuiApplication
>
> if __name__ == '__main__':
>     app = QGuiApplication(sys.argv)
>     engine = QQmlApplicationEngine("main.qml")
>     print(engine)
>     sys.exit(app.exec_())
>
> The print()-statement is never executed, no window is appearing.
>
> What did I miss?
>
> Thanks in advance
>
> Daniel
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160225/88ff6b61/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 10665 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160225/88ff6b61/attachment-0001.png>


More information about the PyQt mailing list