Qt creator python как пользоваться

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. You will need Qt Creator to design and modify your interface (UI file).

If you don’t know how to use Qt Creator, refer to the Using Qt Designer documentation page.

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:

  version="4.0"> MainWindow  class="QMainWindow" name="MainWindow">  name="geometry">  0 0 400 300    name="windowTitle"> MainWindow   class="QWidget" name="centralWidget">  class="QPushButton" name="pushButton">  name="geometry">  110 80 201 81    name="text"> PushButton     class="QMenuBar" name="menuBar">  name="geometry">  0 0 400 20     class="QToolBar" name="mainToolBar">  name="toolBarArea"> TopToolBarArea   name="toolBarBreak"> false    class="QStatusBar" name="statusBar"/>   spacing="6" margin="11"/>    

Now we are ready to decide how to use the UI file from Python.

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. To use this tool, you need to run the following command on a console:

pyside2-uic mainwindow.ui > ui_mainwindow.py 

We redirect all the output of the command to a file called ui_mainwindow.py , which will be imported directly:

from ui_mainwindow import Ui_MainWindow 

Now to use it, we should create a personalized class for our widget to setup this generated design.

To understand the idea, let’s take a look at the whole code:

import sys from PySide2.QtWidgets import QApplication, QMainWindow from PySide2.QtCore import QFile from ui_mainwindow import Ui_MainWindow class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) 

What is inside the if statement is already known from the previous examples, and our new basic class contains only two new lines that are in charge of loading the generated python class from the UI file:

self.ui = Ui_MainWindow() self.ui.setupUi(self) 

You must run pyside2-uic again every time you make changes to the UI file.

Option B: Loading it directly¶

To load the UI file directly, we will need a class from the QtUiTools module:

from PySide2.QtUiTools import QUiLoader 

The QUiLoader lets us load the ui file dynamically and use it right away:

ui_file = QFile("mainwindow.ui") ui_file.open(QFile.ReadOnly) loader = QUiLoader() window = loader.load(ui_file) window.show() 

The complete code of this example looks like this:

# File: main.py import sys from PySide2.QtUiTools import QUiLoader from PySide2.QtWidgets import QApplication from PySide2.QtCore import QFile, QIODevice if __name__ == "__main__": app = QApplication(sys.argv) 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_()) 

Then to execute it we just need to run the following on a command prompt:

QUiLoader uses connect() calls taking the function signatures as string arguments for signal/slot connections. It is thus unable to handle Python types like str or list from custom widgets written in Python since these types are internally mapped to different C++ types.

© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.

Источник

Qt for Python/GettingStarted

Refer to the official docs to start building and using Qt for Python.

Using Qt Creator as a project explorer

Qt Creator 4.0+ can be used to open the PySide and Shiboken CMakeLists.txt files as projects, and thus provide usual IDE features for developing PySide — project file navigation, code completion (C++ only), following symbols under cursor (C++ only), syntax highlighting, locator usage, debugging, etc.

Currently, there is a limitation that Shiboken has to be built first using the terminal because the installed shiboken CMake packages will have to be specified for the PySide project in Qt Creator.

The steps for opening the projects in Qt Creator are:

  1. Open pyside-setup/sources/shiboken2/CMakeLists.txt and specify a 5.12+ Qt Kit to be used
  2. Build the project as usual (by pressing the build icon for instance)
  3. Open pyside-setup/sources/pyside2/CMakeLists.txt and specify the same 5.12+ Qt Kit
  4. Go to the projects tab, and under the Build / CMake section find the Shiboken2_DIR setting. You have to specify the path to the folder where the Shiboken CMake package was installed when you compiled Shiboken from the terminal·
  5. An example path under macOS is /Users/user/Dev/pyside2-setup/pyside_install/py3.6-qt5.12.1-64bit-debug/lib/cmake/Shiboken2-2.0.0. The path has to be adjusted depending on the user folder name, the version of python and qt, etc
  6. (Optional) On MacOS you also have to set the ALTERNATIVE_QT_INCLUDE_DIR setting to the Qt kit include path (e.g. /Users/user/Dev/qt511_source/include)
  7. Apply the CMake configuration changes (by pressing the button), and you should be able to build PySide

Now you can use the project explorer to look through the source cpp files, python files, use the locator feature to open files and file classes / methods, and other features that Qt Creator provides.

Troubleshooting / Known Issues

Оцените статью