[PyQt] PyQT5, QML and grid layout problems on OS X.

M Jérôme LAURENS jerome.laurens at u-bourgogne.fr
Tue Jul 19 22:11:18 BST 2016


Hi there,

Sorry for being long but that is a very annoying problem.

I started from a basic tutorial on using PyQT quick application available at

http://ceg.developpez.com/tutoriels/pyqt/qt-quick-python/02-interaction-qml-python/

Basically it creates a UI with label and buttons and interface a qml file with python.
My first problem was that python did not find the qml modules, I had to insert something like

engine = QQmlApplicationEngine()
engine.addImportPath(".../Qt/5.6/clang_64/qml/")

or use  QML2_IMPORT_PATH=".../Qt/5.6/clang_64/qml"

First questions : is it the expected way of declaring qml module location ?
What about other platforms ? (my app will target iOS, android…)
What about deployment ?

Once qml is properly configured from the python side, I can observe that grid layout is not working as expected.

Below is a minimal (non) working example. qmlscene shows 4 labels on a 2x2 grid and this is exactly what I obtain with the cpp version. But the pyqt5 version displays both labels at the same place, one above the other like on a 1x1 grid. I reinstalled pyqt5 from source, no change.

TIA

main.qml:

import QtQuick 2.5
import QtQuick.Layouts 1.0
import QtQuick.Controls 1.3

ApplicationWindow {
    visible: true
    width: 100
    height: 100
    GridLayout {
        columns: 2
        Label {
            text: "Left"
        }
        Label {
            text: "Right"
        }
        Label {
            text: "Left"
        }
        Label {
            text: "Right"
        }
    }
}

main.cpp:

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QLatin1String("qrc:/main.qml")));

    return app.exec();
}


main.py:

 #!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtCore import QFileInfo, QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
import PyQt5.QtQuick

_root = QFileInfo(__file__).absolutePath()
_root_url = 'qrc:' if _root.startswith(':') else _root

if __name__ == "__main__":
    app = QApplication(sys.argv)
    engine = QQmlApplicationEngine()
    engine.addImportPath(".../Qt/5.6/clang_64/qml/")
    engine.load(_root_url+'/main.qml')
    sys.exit(app.exec_())



More information about the PyQt mailing list