aespid
aespid

Reputation: 73

Pyside2 - qml returns an empty window

I am trying to use QML with pyside2 but am running in some issues. I create a simple example to show the issue I am having. I made a new qml project in Qt Design Studio and am trying to load the qml file in pyside2 but when I run the script, it returns an empty window.

I tried to move the qml at the same level as my main.py but am getting the same results. my tree structure looks like this.

this is the content of my main.py

from PySide2.QtWidgets import QApplication
from PySide2.QtQuick import QQuickView
from PySide2.QtCore import QUrl
from PySide2.QtGui import QGuiApplication
import sys

path = 'qmlTest01/qmlTest01.qml'
app = QGuiApplication(sys.argv)
view = QQuickView()
url = QUrl.fromLocalFile(path)

view.setSource(url)
view.show()
app.exec_()

I am getting an empty window like this:

instead of this:

Thank you in advance.

I made a repo with this example: https://bitbucket.org/aespid/tests/src

Upvotes: 1

Views: 517

Answers (1)

eyllanesc
eyllanesc

Reputation: 244282

Explanation:

Your project structure is as follows:

├── main.py
└── qmlTest01
    ├── imports
    │   ├── qmlTest01
    │   │   └── ...
    │   └── QtQuick
    │       └── ...
    ├── qmlTest01.qml
    ├── qmlTest01.qmlproject
    ├── qtquickcontrols2.conf
    └── Screen01.ui.qml

And in your imports you use:

import qmlTest01 1.0
// ...

but nowhere do you indicate where that library is, but in the case of the second image you must be running the .qmlproject from QtCreator that reads the following configuration:

/* File generated by Qt Creator */

import QmlProject 1.1

Project {
    mainFile: "qmlTest01.qml"

    /* ... */

    Files {
        filter: "qmldir"
        directory: "."
    }

    Environment {
        QT_QUICK_CONTROLS_CONF: "qtquickcontrols2.conf"
        QT_AUTO_SCREEN_SCALE_FACTOR: "1"
    }

    /* List of plugin directories passed to QML runtime */
    importPaths: [ "imports" ]

    /* Required for deployment */
    targetDirectory: "/opt/qmlTest01"
}

Where it is observed that indicates the route of the import of other modules.

Solution:

The solution is to set some environment variables with os.environ() and the path of the "imports" folder using addImportPath() of QQmlEngine:

import os
import sys

from PySide2.QtCore import QUrl
from PySide2.QtGui import QGuiApplication
from PySide2.QtQuick import QQuickView


if __name__ == "__main__":

    current_dir = os.path.dirname(os.path.realpath(__file__))

    os.environ["QT_QUICK_CONTROLS_CONF"] = os.path.join(
        current_dir, "qmlTest01", "qtquickcontrols2.conf"
    )
    os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"

    app = QGuiApplication(sys.argv)
    view = QQuickView()
    import_path = os.path.join(current_dir, "qmlTest01", "imports")
    view.engine().addImportPath(import_path)

    filename = os.path.join(current_dir, "qmlTest01", "qmlTest01.qml")
    url = QUrl.fromLocalFile(filename)

    view.setSource(url)
    view.show()
    app.exec_()

Upvotes: 1

Related Questions