How to use Qt Creator to design Graphical Interfaces for PySide

This page describes the use of Qt Creator to create Graphical Interfaces for your PySide Software. You will need Qt Creator, Pyside and PySide-Tools (pyuic and pyrcc).

If you don’t know how to use Qt Creator, please go to Qt Creator (ver 2.5) Widget Tutorial [qt-project.org].

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. You should get somethig like this :

mainwindow

And your mainwindow.ui

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3.  <class>MainWindow</class>
  4.  <widget class="QMainWindow" name="MainWindow">
  5.   <property name="geometry">
  6.    <rect>
  7.     <x>0</x>
  8.     <y>0</y>
  9.     <width>82</width>
  10.     <height>64</height>
  11.    </rect>
  12.   </property>
  13.   <property name="windowTitle">
  14.    <string>MainWindow</string>
  15.   </property>
  16.   <widget class="QWidget" name="centralwidget">
  17.    <layout class="QGridLayout" name="gridLayout">
  18.     <item row="0" column="0">
  19.      <widget class="QLabel" name="label">
  20.       <property name="text">
  21.        <string>Hello World!</string>
  22.       </property>
  23.      </widget>
  24.     </item>
  25.    </layout>
  26.   </widget>
  27.   <widget class="QMenuBar" name="menubar">
  28.    <property name="geometry">
  29.     <rect>
  30.      <x>0</x>
  31.      <y>0</y>
  32.      <width>82</width>
  33.      <height>21</height>
  34.     </rect>
  35.    </property>
  36.   </widget>
  37.   <widget class="QStatusBar" name="statusbar"/>
  38.  </widget>
  39.  <resources/>
  40.  <connections/>
  41. </ui>

Python can’t read this ui file so to make it readable to python we need to process it with a script pyside-uic.
The command is:
  1. pyside-uic mainwindow.ui  -o mainwindow.py

The generated output file “mainwindow.py” is as follows:

  1. # -*- coding: utf-8 -*-
  2. # Form implementation generated from reading ui file 'mainwindow.ui'
  3. #
  4. # Created: Mon Dec 10 16:22:24 2012
  5. #      by: pyside-uic 0.2.13 running on PySide 1.1.0
  6. #
  7. # WARNING! All changes made in this file will be lost!
  8.                                                                                                                                                                                                          
  9. from PySide import QtCore, QtGui
  10.  
  11. class Ui_MainWindow(object):
  12.     def setupUi(self, MainWindow):
  13.         MainWindow.setObjectName("MainWindow")
  14.         MainWindow.resize(82, 64)
  15.         self.centralwidget = QtGui.QWidget(MainWindow)
  16.         self.centralwidget.setObjectName("centralwidget")
  17.         self.gridLayout = QtGui.QGridLayout(self.centralwidget)
  18.         self.gridLayout.setObjectName("gridLayout")
  19.         self.label = QtGui.QLabel(self.centralwidget)
  20.         self.label.setObjectName("label")
  21.         self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
  22.         MainWindow.setCentralWidget(self.centralwidget)
  23.         self.menubar = QtGui.QMenuBar(MainWindow)
  24.         self.menubar.setGeometry(QtCore.QRect(0, 0, 82, 21))
  25.         self.menubar.setObjectName("menubar")
  26.         MainWindow.setMenuBar(self.menubar)
  27.         self.statusbar = QtGui.QStatusBar(MainWindow)
  28.         self.statusbar.setObjectName("statusbar")
  29.         MainWindow.setStatusBar(self.statusbar)
  30.  
  31.         self.retranslateUi(MainWindow)
  32.         QtCore.QMetaObject.connectSlotsByName(MainWindow)
  33.  
  34.     def retranslateUi(self, MainWindow):
  35.         MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
  36.         self.label.setText(QtGui.QApplication.translate("MainWindow", "Hello World!", None, QtGui.QApplication.UnicodeUTF8))

we have to modify this mainwindow.py module:
  1. # -*- coding: utf-8 -*-
  2.  
  3. # Form implementation generated from reading ui file 'mainwindow.ui'
  4. #
  5. # Created: Mon Dec 10 16:22:24 2012
  6. # by: pyside-uic 0.2.13 running on PySide 1.1.0
  7. #
  8. # WARNING! All changes made in this file will be lost!
  9.  
  10. import sys
  11. from PySide import QtCore, QtGui
  12.  
  13. class Ui_MainWindow(object):
  14.   def setupUi(self, MainWindow):
  15.     MainWindow.setObjectName("MainWindow")
  16.     MainWindow.resize(82, 64)
  17.     self.centralwidget = QtGui.QWidget(MainWindow)
  18.     self.centralwidget.setObjectName("centralwidget")
  19.     self.gridLayout = QtGui.QGridLayout(self.centralwidget)
  20.     self.gridLayout.setObjectName("gridLayout")
  21.     self.label = QtGui.QLabel(self.centralwidget)
  22.     self.label.setObjectName("label")
  23.     self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
  24.     MainWindow.setCentralWidget(self.centralwidget)
  25.     self.menubar = QtGui.QMenuBar(MainWindow)
  26.     self.menubar.setGeometry(QtCore.QRect(0, 0, 82, 21))
  27.     self.menubar.setObjectName("menubar")
  28.     MainWindow.setMenuBar(self.menubar)
  29.     self.statusbar = QtGui.QStatusBar(MainWindow)
  30.     self.statusbar.setObjectName("statusbar")
  31.     MainWindow.setStatusBar(self.statusbar)
  32.  
  33.     self.retranslateUi(MainWindow)
  34.     QtCore.QMetaObject.connectSlotsByName(MainWindow)
  35.  
  36.   def retranslateUi(self, MainWindow):
  37.     MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
  38.     self.label.setText(QtGui.QApplication.translate("MainWindow", "Hello World!", None, QtGui.QApplication.UnicodeUTF8))
  39.  
  40.    
  41. class ControlMainWindow(QtGui.QMainWindow):
  42.   def __init__(self, parent=None):
  43.     super(ControlMainWindow, self).__init__(parent)
  44.     self.ui =  Ui_MainWindow()
  45.     self.ui.setupUi(self)
  46.    
  47. if __name__ == "__main__":
  48.     app = QtGui.QApplication(sys.argv)
  49.     mySW = ControlMainWindow()
  50.     mySW.show()
  51.     sys.exit(app.exec_())

And to run:

  1. python mainwindow.py

Categories: