[PyQt] QQmlApplicationEngine and QGuiApplication

Daniel Krause madakr at web.de
Thu Feb 25 21:25:11 GMT 2016


Thank you for your suggestions.

@Jerome
I used you code "as is", but for the last lines: I changed them to

if __name__ == '__main__':
    app = MyQmlApplication("MyAppTitle", sys.argv)
    app.showAndExec(QUrl("main.qml"))

The code executes, but the Window is empty.

[image: Inline-Bild 1]

@Miguel:
I changed additionally the name "MainForm" to "Rectangle" as you suggested.
But still, the window is empty...

I have the feeling, that the qml-file is not found / loaded. If I change
the file name to "Main.qml" (which does not exist), the window looks the
same...
Could it be an OS-problem, as I am working on windows?

Any ideas would be welcome.

Daniel

2016-02-25 18:37 GMT+01:00 Jérôme Godbout <jerome at bodycad.com>:

> 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:
>>
>>
>> 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/f0701502/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 7969 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160225/f0701502/attachment.png>


More information about the PyQt mailing list