[PyQt] QQmlApplicationEngine and QGuiApplication

Daniel Krause madakr at web.de
Thu Feb 25 15:43:19 GMT 2016


I am new to PyQt and having trouble to include QML-Code into an python

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")

The print()-statement is never executed, no window is appearing.

What did I miss?

Thanks in advance

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160225/882a0f95/attachment.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/882a0f95/attachment.png>

More information about the PyQt mailing list