Qt Design Studio + Python code?
-
Hi,
I would like to design a modern-looking QtQuick UI in a visual editor (e.g. QT Design Studio), and to program the application logic in Python. After several days, I cannot get it to work. I found many posts here struggling with the same goal. Is this workflow even viable?
a summary of where I'm at:
-
I can get a simple hand-crafted QML example loaded in python code and successfully run it without problem.
-
I created a simple "empty desktop project" in Qt Design Studio, and inside the Studio the preview button runs it correctly (this "empty project" contains a simple button that changes a text label).
-
I tried to load the QML that Design Studio created. It complained about "module "TestProject2" is not installed", and I found out I need to add the project's folder as an import path.
-
now it advanced further: it does recognize my TestProject2 module but says 'module "QtQuick.Studio.Application" is not installed'. So I found in my Qt Design Studio installation folder that this module resides in:
C:\Qt\Tools\QtDesignStudio\qt6_design_studio_reduced_version\qml so I added this folder too as an import path. I'm not sure that was correct. -
now the line of code that loads the QML crashes the program with exit code 0xC0000409. Googling that suggests that it's some kind of a stack overflow error. I have no idea how to debug that.
my python code is attached below for reference. The QML is the trivial one created when creating an empty desktop project in QT Design Studio.
Any ideas on how to get this to work? I don't want to give up the Studio visual editing and hand-craft the QML. should I give up and go back to traditional GUIs with QTWidgets instead of modern QtQuick? At least the workflow with QTWidgets and Qt Designer (not Studio) was straightforward...
# This Python file uses the following encoding: utf-8 import sys from pathlib import Path from PySide6.QtCore import QUrl from PySide6.QtGui import QGuiApplication from PySide6.QtQml import QQmlApplicationEngine, QQmlComponent if __name__ == "__main__": app = QGuiApplication(sys.argv) engine = QQmlApplicationEngine() cur_dir = Path(__file__).resolve().parent qml_file_path = cur_dir / "TestProject2Content" / "App.qml" proj_import_path = cur_dir lib_import_path = r"C:\Qt\Tools\QtDesignStudio\qt6_design_studio_reduced_version\qml" print(f"project import path: {proj_import_path}") engine.addImportPath(proj_import_path) print(f"Qt lib QML import path: {lib_import_path}") engine.addImportPath(lib_import_path) component = QQmlComponent(engine, QUrl.fromLocalFile(qml_file_path)) # load the QML if component.status() == QQmlComponent.Error: print("Error: Failed to load QML file.") for error in component.errors(): print(error.toString()) sys.exit(-1) sys.exit(app.exec())
-
-
Hey @tinkerer42 this is a feature that will be announced in the following days.
There are a couple of things that you need to keep in mind:- Qt Design Studio provides a few ad-hoc QtQuick components that are not found in Qt installations, but only within Qt DS. For that, we recently uploaded a package that you can get via
pip install pyside6_ds
that will copy those components into your PySide6 installation - Having the QtDS components is not enough, you need a specific structure on your main python file, which should look a bit like:
import os import sys from pathlib import Path from PySide6.QtGui import QGuiApplication from PySide6.QtQml import QQmlApplicationEngine if __name__ == '__main__': app = QGuiApplication(sys.argv) engine = QQmlApplicationEngine() app_dir = Path(__file__).parent.parent engine.addImportPath(os.fspath(app_dir)) # import_paths = ["...", "...", ...] # for all the paths you have files. for path in import_paths: engine.addImportPath(os.fspath(app_dir / path)) # url = "/path/to/your/main.qml" engine.load(os.fspath(app_dir/url)) if not engine.rootObjects(): sys.exit(-1) sys.exit(app.exec())
There will be a blog post in the following weeks with the new features that QtDS will have.
- Qt Design Studio provides a few ad-hoc QtQuick components that are not found in Qt installations, but only within Qt DS. For that, we recently uploaded a package that you can get via
-
C CristianMaureira referenced this topic on