From c926ca2321066f0037e828175175712d70945bf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristi=C3=A1n=20Maureira-Fredes?= Date: Fri, 17 Dec 2021 09:40:21 +0100 Subject: [PATCH] python: update wizard code style - Updated the current code to use recommended code style - Modify description of current wizards - Adapt the documentation to reflect the new names of the applications - Adapt exec() call for PySide6 (and leave exec_() for PySide2) Change-Id: Iad662523b8a0e56ac622b21cee60f7928d419354 Reviewed-by: David Schulz --- .../creator-only/creator-projects-creating.qdoc | 8 ++++---- .../templates/wizards/classes/python/wizard.json | 2 +- .../qtforpythonapplication/empty/wizard.json | 2 +- .../qtforpythonapplication/main_empty.py | 6 +++++- .../qtforpythonapplication/main_mainwindow.py | 13 +++++++------ .../qtforpythonapplication/main_qtquick.py | 14 +++++++++++--- .../qtforpythonapplication/main_widget.py | 16 ++++++++++++---- .../mainwindow/wizard.json | 2 +- .../qtquickapplication/wizard.json | 2 +- .../qtforpythonapplication/widget/wizard.json | 4 ++-- 10 files changed, 45 insertions(+), 24 deletions(-) diff --git a/doc/qtcreator/src/projects/creator-only/creator-projects-creating.qdoc b/doc/qtcreator/src/projects/creator-only/creator-projects-creating.qdoc index c89efd5a465..8d0a1cf8ef8 100644 --- a/doc/qtcreator/src/projects/creator-only/creator-projects-creating.qdoc +++ b/doc/qtcreator/src/projects/creator-only/creator-projects-creating.qdoc @@ -122,20 +122,20 @@ to desktop, embedded, and mobile target platforms. \row \li {1,4} Application (Qt for Python) - \li Qt for Python - Empty + \li Empty Application \li Creates a \l{https://doc.qt.io/qtforpython/index.html} {Qt for Python} application that contains only the main code for a QApplication. \row - \li Qt for Python - Window + \li Empty Window \li Creates a Qt for Python application that contains an empty window. \row - \li Qt for Python - Window (UI file) + \li Window UI - Dynamic load \li Creates a Qt for Python application that contains an empty window with a widget-based UI. \row - \li Qt for Python - Qt Quick Application + \li Qt Quick Application - Empty \li Creates a Python project that contains an empty Qt Quick Application. \row diff --git a/share/qtcreator/templates/wizards/classes/python/wizard.json b/share/qtcreator/templates/wizards/classes/python/wizard.json index 7a86aa341e5..d42d8f0ab52 100644 --- a/share/qtcreator/templates/wizards/classes/python/wizard.json +++ b/share/qtcreator/templates/wizards/classes/python/wizard.json @@ -38,7 +38,7 @@ "type": "ComboBox", "data": { - "items": ["", "PySide2", "PySide6", "PyQt5"] + "items": ["", "PySide6", "PySide2", "PyQt6", "PyQt5"] } }, { diff --git a/share/qtcreator/templates/wizards/projects/qtforpythonapplication/empty/wizard.json b/share/qtcreator/templates/wizards/projects/qtforpythonapplication/empty/wizard.json index 268fc6bd9e6..7a032fc0f11 100644 --- a/share/qtcreator/templates/wizards/projects/qtforpythonapplication/empty/wizard.json +++ b/share/qtcreator/templates/wizards/projects/qtforpythonapplication/empty/wizard.json @@ -4,7 +4,7 @@ "id": "F.QtForPythonApplicationEmpty", "category": "F.ApplicationPySide", "trDescription": "Creates a Qt for Python application that contains only the main code for a QApplication.", - "trDisplayName": "Qt for Python - Empty", + "trDisplayName": "Empty Application", "trDisplayCategory": "Application (Qt for Python)", "icon": "icon.png", "iconKind": "Themed", diff --git a/share/qtcreator/templates/wizards/projects/qtforpythonapplication/main_empty.py b/share/qtcreator/templates/wizards/projects/qtforpythonapplication/main_empty.py index ec9a1ffa718..e8a45b363c0 100644 --- a/share/qtcreator/templates/wizards/projects/qtforpythonapplication/main_empty.py +++ b/share/qtcreator/templates/wizards/projects/qtforpythonapplication/main_empty.py @@ -4,6 +4,10 @@ from %{PySideVersion}.QtWidgets import QApplication if __name__ == "__main__": - app = QApplication([]) + app = QApplication(sys.argv) # ... +@if '%{PySideVersion}' === 'PySide6' + sys.exit(app.exec()) +@else sys.exit(app.exec_()) +@endif diff --git a/share/qtcreator/templates/wizards/projects/qtforpythonapplication/main_mainwindow.py b/share/qtcreator/templates/wizards/projects/qtforpythonapplication/main_mainwindow.py index b0afe88e987..102840cc298 100644 --- a/share/qtcreator/templates/wizards/projects/qtforpythonapplication/main_mainwindow.py +++ b/share/qtcreator/templates/wizards/projects/qtforpythonapplication/main_mainwindow.py @@ -16,12 +16,9 @@ class %{Class}(%{BaseCB}): @else class %{Class}: @endif - def __init__(self): -@if '%{BaseCB}' === 'QWidget' - QWidget.__init__(self) -@endif -@if '%{BaseCB}' === 'QMainWindow' - QMainWindow.__init__(self) + def __init__(self, parent=None): +@if '%{BaseCB}' === 'QWidget' || '%{BaseCB}' === 'QMainWindow' + super().__init__(parent) @endif @if '%{BaseCB}' === '' pass # call __init__(self) of the custom base class here @@ -36,4 +33,8 @@ if __name__ == "__main__": @else window.show() @endif +@if '%{PySideVersion}' === 'PySide6' + sys.exit(app.exec()) +@else sys.exit(app.exec_()) +@endif diff --git a/share/qtcreator/templates/wizards/projects/qtforpythonapplication/main_qtquick.py b/share/qtcreator/templates/wizards/projects/qtforpythonapplication/main_qtquick.py index 2bb159fd05c..2c410825c8b 100644 --- a/share/qtcreator/templates/wizards/projects/qtforpythonapplication/main_qtquick.py +++ b/share/qtcreator/templates/wizards/projects/qtforpythonapplication/main_qtquick.py @@ -1,7 +1,6 @@ # This Python file uses the following encoding: utf-8 -import os -from pathlib import Path import sys +from pathlib import Path from %{PySideVersion}.QtGui import QGuiApplication from %{PySideVersion}.QtQml import QQmlApplicationEngine @@ -10,7 +9,16 @@ from %{PySideVersion}.QtQml import QQmlApplicationEngine if __name__ == "__main__": app = QGuiApplication(sys.argv) engine = QQmlApplicationEngine() - engine.load(os.fspath(Path(__file__).resolve().parent / "%{QmlFileName}")) + qml_file = Path(__file__).resolve().parent / "%{QmlFileName}" +@if '%{PySideVersion}' === 'PySide6' + engine.load(qml_file) +@else + engine.load(str(qml_file)) +@endif if not engine.rootObjects(): sys.exit(-1) +@if '%{PySideVersion}' === 'PySide6' + sys.exit(app.exec()) +@else sys.exit(app.exec_()) +@endif diff --git a/share/qtcreator/templates/wizards/projects/qtforpythonapplication/main_widget.py b/share/qtcreator/templates/wizards/projects/qtforpythonapplication/main_widget.py index e00ab451e5d..02b7ab79115 100644 --- a/share/qtcreator/templates/wizards/projects/qtforpythonapplication/main_widget.py +++ b/share/qtcreator/templates/wizards/projects/qtforpythonapplication/main_widget.py @@ -21,21 +21,29 @@ class %{Class}(%{BaseCB}): @else class %{Class}: @endif - def __init__(self): - super(%{Class}, self).__init__() + def __init__(self, parent=None): + super().__init__(parent) self.load_ui() def load_ui(self): loader = QUiLoader() - path = os.fspath(Path(__file__).resolve().parent / "form.ui") + path = Path(__file__).resolve().parent / "form.ui" +@if '%{PySideVersion}' === 'PySide6' ui_file = QFile(path) +@else + ui_file = QFile(str(path)) +@endif ui_file.open(QFile.ReadOnly) loader.load(ui_file, self) ui_file.close() if __name__ == "__main__": - app = QApplication([]) + app = QApplication(sys.argv) widget = %{Class}() widget.show() +@if '%{PySideVersion}' === 'PySide6' + sys.exit(app.exec()) +@else sys.exit(app.exec_()) +@endif diff --git a/share/qtcreator/templates/wizards/projects/qtforpythonapplication/mainwindow/wizard.json b/share/qtcreator/templates/wizards/projects/qtforpythonapplication/mainwindow/wizard.json index a16b7fd900a..1e19a607f71 100644 --- a/share/qtcreator/templates/wizards/projects/qtforpythonapplication/mainwindow/wizard.json +++ b/share/qtcreator/templates/wizards/projects/qtforpythonapplication/mainwindow/wizard.json @@ -4,7 +4,7 @@ "id": "F.QtForPythonApplicationWindow", "category": "F.ApplicationPySide", "trDescription": "Creates a Qt for Python application that contains an empty window.", - "trDisplayName": "Qt for Python - Window", + "trDisplayName": "Empty Window", "trDisplayCategory": "Application (Qt for Python)", "icon": "../icons/icon.png", "iconKind": "Themed", diff --git a/share/qtcreator/templates/wizards/projects/qtforpythonapplication/qtquickapplication/wizard.json b/share/qtcreator/templates/wizards/projects/qtforpythonapplication/qtquickapplication/wizard.json index 268367d1f6f..68051e52f78 100644 --- a/share/qtcreator/templates/wizards/projects/qtforpythonapplication/qtquickapplication/wizard.json +++ b/share/qtcreator/templates/wizards/projects/qtforpythonapplication/qtquickapplication/wizard.json @@ -4,7 +4,7 @@ "id": "F.QtQuickQtForPythonApplicationEmpty", "category": "F.ApplicationPySide", "trDescription": "Creates a Qt Quick application that contains an empty window.", - "trDisplayName": "Qt for Python - Qt Quick Application - Empty", + "trDisplayName": "Qt Quick Application - Empty", "trDisplayCategory": "Application (Qt for Python)", "icon": "../icons/icon.png", "iconKind": "Themed", diff --git a/share/qtcreator/templates/wizards/projects/qtforpythonapplication/widget/wizard.json b/share/qtcreator/templates/wizards/projects/qtforpythonapplication/widget/wizard.json index 6a068d5fd3c..6b2bd45565f 100644 --- a/share/qtcreator/templates/wizards/projects/qtforpythonapplication/widget/wizard.json +++ b/share/qtcreator/templates/wizards/projects/qtforpythonapplication/widget/wizard.json @@ -3,8 +3,8 @@ "supportedProjectTypes": [ "PythonProject" ], "id": "F.QtForPythonApplicationWindowWidget", "category": "F.ApplicationPySide", - "trDescription": "Creates a Qt for Python application that includes a Qt Designer-based widget (ui file)", - "trDisplayName": "Qt for Python - Window (UI file)", + "trDescription": "Creates a Qt for Python application that includes a Qt Designer-based widget (.ui file) - Load the forms dynamically/at runtime", + "trDisplayName": "Window UI - Dynamic load", "trDisplayCategory": "Application (Qt for Python)", "icon": "../icons/icon.png", "iconKind": "Themed",