Qt for Python UiFiles

From Qt Wiki
Revision as of 15:17, 27 May 2018 by AutoSpider (talk | contribs) (Move Category:QtForPython -> Category:Qt for Python)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This page describes the use of Qt Creator to create Graphical Interfaces for your PySide Software. You will need Qt Creator to design and modify your interface (ui file)

If you don't know how to use Qt Creator, please go to Using Qt Designer.

At Qt Creator, create a new Qt Design Form, choose "Main Window" for template. And save as "mainwindow.ui". Add a Qlabel to the center of the centralwidget.

Your file (mainwindow.ui) should look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <widget class="QMainWindow" name="MainWindow">
 <property name="geometry">
 <property name="windowTitle">
 <widget class="QWidget" name="centralwidget">
 <layout class="QGridLayout" name="gridLayout">
 <item row="0" column="0">
 <widget class="QLabel" name="label">
 <property name="text">
 <string>Hello World!</string>
 <widget class="QMenuBar" name="menubar">
 <property name="geometry">
 <widget class="QStatusBar" name="statusbar"/>

Now, using PySide2 we will load the ui file and start our application:

# main.py
import sys
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import QFile

if __name__ == "__main__":
    app = QApplication(sys.argv)

    file = QFile("mainwindow.ui")

    loader = QUiLoader()
    window = loader.load(file)


And to run it, just a

python main.py

will do the job.