aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside2/doc/tutorials/basictutorial/uifiles.rst
diff options
context:
space:
mode:
Diffstat (limited to 'sources/pyside2/doc/tutorials/basictutorial/uifiles.rst')
-rw-r--r--sources/pyside2/doc/tutorials/basictutorial/uifiles.rst30
1 files changed, 18 insertions, 12 deletions
diff --git a/sources/pyside2/doc/tutorials/basictutorial/uifiles.rst b/sources/pyside2/doc/tutorials/basictutorial/uifiles.rst
index a45bfc18c..982384629 100644
--- a/sources/pyside2/doc/tutorials/basictutorial/uifiles.rst
+++ b/sources/pyside2/doc/tutorials/basictutorial/uifiles.rst
@@ -1,5 +1,5 @@
-Using UI Files
-***************
+Using `.ui` files from Designer or QtCreator with `QUiLoader` and `pyside2-uic`
+*******************************************************************************
This page describes the use of Qt Creator to create graphical
interfaces for your Qt for Python project.
@@ -13,8 +13,9 @@ At Qt Creator, create a new Qt Design Form, choose "Main Window" for template.
And save as `mainwindow.ui`.
Add a `QPushButton` to the center of the centralwidget.
-Your file (mainwindow.ui) should look something like this:
-::
+Your file ``mainwindow.ui`` should look something like this:
+
+.. code-block:: xml
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
@@ -73,8 +74,8 @@ Your file (mainwindow.ui) should look something like this:
Now we are ready to decide how to use the **UI file** from Python.
-Generating a Python class
-=========================
+Option A: Generating a Python class
+===================================
Another option to interact with a **UI file** is to generate a Python
class from it. This is possible thanks to the `pyside2-uic` tool.
@@ -128,8 +129,8 @@ file:
You must run `pyside2-uic` again every time you make changes
to the **UI file**.
-Loading it directly
-====================
+Option B: Loading it directly
+=============================
To load the UI file directly, we will need a class from the **QtUiTools**
module:
@@ -158,17 +159,22 @@ The complete code of this example looks like this:
import sys
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication
- from PySide2.QtCore import QFile
+ from PySide2.QtCore import QFile, QIODevice
if __name__ == "__main__":
app = QApplication(sys.argv)
- ui_file = QFile("mainwindow.ui")
- ui_file.open(QFile.ReadOnly)
-
+ ui_file_name = "mainwindow.ui"
+ ui_file = QFile(ui_file_name)
+ if not ui_file.open(QIODevice.ReadOnly):
+ print("Cannot open {}: {}".format(ui_file_name, ui_file.errorString()))
+ sys.exit(-1)
loader = QUiLoader()
window = loader.load(ui_file)
ui_file.close()
+ if not window:
+ print(loader.errorString())
+ sys.exit(-1)
window.show()
sys.exit(app.exec_())