Qt for Python UiFiles: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 5: Line 5:
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).
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 ":http://doc.qt.io/qtcreator-2.5/creator-writing-program.html.
If you don't know how to use Qt Creator, please go to "Qt Creator (ver 2.5) Widget Tutorial ":http://doc.qt.io/qtcreator-2.5/creator-writing-program.html.


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 :
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 :


[[Image:https://dl.dropbox.com/u/3490869/mainwindow.png|mainwindow]]
[[Image:https://dl.dropbox.com/u/3490869/mainwindow.png|mainwindow]]


And your mainwindow.ui<br /><code><br />&amp;lt;?xml version=&quot;1.0&amp;quot; encoding=&quot;UTF-8&amp;quot;?&amp;gt;<br />&lt;ui version=&quot;4.0&amp;quot;&gt;<br /> &lt;class&amp;gt;MainWindow&amp;lt;/class&amp;gt;<br /> &lt;widget class=&quot;QMainWindow&amp;quot; name=&quot;MainWindow&amp;quot;&gt;<br /> &lt;property name=&quot;geometry&amp;quot;&gt;<br /> &lt;rect&amp;gt;<br /> &lt;x&amp;gt;0&amp;lt;/x&amp;gt;<br /> &lt;y&amp;gt;0&amp;lt;/y&amp;gt;<br /> &lt;width&amp;gt;82&amp;lt;/width&amp;gt;<br /> &lt;height&amp;gt;64&amp;lt;/height&amp;gt;<br /> &lt;/rect&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;windowTitle&amp;quot;&gt;<br /> &lt;string&amp;gt;MainWindow&amp;lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;widget class=&quot;QWidget&amp;quot; name=&quot;centralwidget&amp;quot;&gt;<br /> &lt;layout class=&quot;QGridLayout&amp;quot; name=&quot;gridLayout&amp;quot;&gt;<br /> &lt;item row=&quot;0&amp;quot; column=&quot;0&amp;quot;&gt;<br /> &lt;widget class=&quot;QLabel&amp;quot; name=&quot;label&amp;quot;&gt;<br /> &lt;property name=&quot;text&amp;quot;&gt;<br /> &lt;string&amp;gt;Hello World!&lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/item&amp;gt;<br /> &lt;/layout&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;widget class=&quot;QMenuBar&amp;quot; name=&quot;menubar&amp;quot;&gt;<br /> &lt;property name=&quot;geometry&amp;quot;&gt;<br /> &lt;rect&amp;gt;<br /> &lt;x&amp;gt;0&amp;lt;/x&amp;gt;<br /> &lt;y&amp;gt;0&amp;lt;/y&amp;gt;<br /> &lt;width&amp;gt;82&amp;lt;/width&amp;gt;<br /> &lt;height&amp;gt;21&amp;lt;/height&amp;gt;<br /> &lt;/rect&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;widget class=&quot;QStatusBar&amp;quot; name=&quot;statusbar&amp;quot;/&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;resources/&amp;gt;<br /> &lt;connections/&amp;gt;<br />&lt;/ui&amp;gt;<br /></code><br />Python can't read this ui file so to make it readable to python we need to process it with a script pyside-uic.<br />The command is:<br /><code><br />pyside-uic mainwindow.ui <s>o mainwindow.py<br /></code>
And your mainwindow.ui
<br />The generated output file &quot;mainwindow.py&amp;quot; is as follows:<br /><code><br />#</s>*- coding: utf-8 <s>*</s><br /># Form implementation generated from reading ui file 'mainwindow.ui'<br />#<br /># Created: Mon Dec 10 16:22:24 2012<br /># by: pyside-uic 0.2.13 running on PySide 1.1.0<br />#<br /># WARNING! All changes made in this file will be lost!
<code>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>82</width>
<height>64</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<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>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>82</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
</code>
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:
<code>
pyside-uic mainwindow.ui -o mainwindow.py
</code>
 
The generated output file "mainwindow.py" is as follows:
<code>
#-*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Mon Dec 10 16:22:24 2012
# by: pyside-uic 0.2.13 running on PySide 1.1.0
#
# WARNING! All changes made in this file will be lost!


from PySide import QtCore, QtGui
from PySide import QtCore, QtGui


class Ui_MainWindow(object):<br /> def setupUi(self, MainWindow):<br /> MainWindow.setObjectName(&quot;MainWindow&amp;quot;)<br /> MainWindow.resize(82, 64)<br /> self.centralwidget = QtGui.QWidget(MainWindow)<br /> self.centralwidget.setObjectName(&quot;centralwidget&amp;quot;)<br /> self.gridLayout = QtGui.QGridLayout(self.centralwidget)<br /> self.gridLayout.setObjectName(&quot;gridLayout&amp;quot;)<br /> self.label = QtGui.QLabel(self.centralwidget)<br /> self.label.setObjectName(&quot;label&amp;quot;)<br /> self.gridLayout.addWidget(self.label, 0, 0, 1, 1)<br /> MainWindow.setCentralWidget(self.centralwidget)<br /> self.menubar = QtGui.QMenuBar(MainWindow)<br /> self.menubar.setGeometry(QtCore.QRect(0, 0, 82, 21))<br /> self.menubar.setObjectName(&quot;menubar&amp;quot;)<br /> MainWindow.setMenuBar(self.menubar)<br /> self.statusbar = QtGui.QStatusBar(MainWindow)<br /> self.statusbar.setObjectName(&quot;statusbar&amp;quot;)<br /> MainWindow.setStatusBar(self.statusbar)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(82, 64)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.label = QtGui.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 82, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)


self.retranslateUi(MainWindow)<br /> QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)


def retranslateUi(self, MainWindow):<br /> MainWindow.setWindowTitle(QtGui.QApplication.translate(&quot;MainWindow&amp;quot;, &quot;MainWindow&amp;quot;, None, QtGui.QApplication.UnicodeUTF8))<br /> self.label.setText(QtGui.QApplication.translate(&quot;MainWindow&amp;quot;, &quot;Hello World!&quot;, None, QtGui.QApplication.UnicodeUTF8))
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("MainWindow", "Hello World!", None, QtGui.QApplication.UnicodeUTF8))


</code><br />we have to modify this mainwindow.py module:<br /><code><br /># <s>*</s> coding: utf-8 <s>*</s>
</code>
we have to modify this mainwindow.py module:
<code>
# -*- coding: utf-8 -*-


# Form implementation generated from reading ui file 'mainwindow.ui'  
# Form implementation generated from reading ui file 'mainwindow.ui'  
Line 31: Line 112:
# WARNING! All changes made in this file will be lost!  
# WARNING! All changes made in this file will be lost!  


import sys<br />from PySide import QtCore, QtGui
import sys
from PySide import QtCore, QtGui


class Ui_MainWindow(object):<br /> def setupUi(self, MainWindow):<br /> MainWindow.setObjectName(&quot;MainWindow&amp;quot;)<br /> MainWindow.resize(82, 64)<br /> self.centralwidget = QtGui.QWidget(MainWindow)<br /> self.centralwidget.setObjectName(&quot;centralwidget&amp;quot;)<br /> self.gridLayout = QtGui.QGridLayout(self.centralwidget)<br /> self.gridLayout.setObjectName(&quot;gridLayout&amp;quot;)<br /> self.label = QtGui.QLabel(self.centralwidget)<br /> self.label.setObjectName(&quot;label&amp;quot;)<br /> self.gridLayout.addWidget(self.label, 0, 0, 1, 1)<br /> MainWindow.setCentralWidget(self.centralwidget)<br /> self.menubar = QtGui.QMenuBar(MainWindow)<br /> self.menubar.setGeometry(QtCore.QRect(0, 0, 82, 21))<br /> self.menubar.setObjectName(&quot;menubar&amp;quot;)<br /> MainWindow.setMenuBar(self.menubar)<br /> self.statusbar = QtGui.QStatusBar(MainWindow)<br /> self.statusbar.setObjectName(&quot;statusbar&amp;quot;)<br /> MainWindow.setStatusBar(self.statusbar)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(82, 64)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.label = QtGui.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 82, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)


self.retranslateUi(MainWindow)<br /> QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)


def retranslateUi(self, MainWindow):<br /> MainWindow.setWindowTitle(QtGui.QApplication.translate(&quot;MainWindow&amp;quot;, &quot;MainWindow&amp;quot;, None, QtGui.QApplication.UnicodeUTF8))<br /> self.label.setText(QtGui.QApplication.translate(&quot;MainWindow&amp;quot;, &quot;Hello World!&quot;, None, QtGui.QApplication.UnicodeUTF8))
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("MainWindow", "Hello World!", None, QtGui.QApplication.UnicodeUTF8))


class ControlMainWindow(QtGui.QMainWindow):<br /> def ''init''(self, parent=None):<br /> super(ControlMainWindow, self).''init''(parent)<br /> self.ui = Ui_MainWindow()<br /> self.ui.setupUi(self)
class ControlMainWindow(QtGui.QMainWindow):
def ''init''(self, parent=None):
super(ControlMainWindow, self).''init''(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)


if ''name'' == &quot;''main''&quot;:<br /> app = QtGui.QApplication(sys.argv)<br /> mySW = ControlMainWindow()<br /> mySW.show()<br /> sys.exit(app.exec_())<br /></code>
if ''name'' == "''main''":
app = QtGui.QApplication(sys.argv)
mySW = ControlMainWindow()
mySW.show()
sys.exit(app.exec_())
</code>


And to run:<br /><code><br />python mainwindow.py
And to run:
<code>
python mainwindow.py

Revision as of 08:37, 25 February 2015


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 ":http://doc.qt.io/qtcreator-2.5/creator-writing-program.html.

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

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
 <property name="geometry">
 <rect>
 <x>0</x>
 <y>0</y>
 <width>82</width>
 <height>64</height>
 </rect>
 </property>
 <property name="windowTitle">
 <string>MainWindow</string>
 </property>
 <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>
 </property>
 </widget>
 </item>
 </layout>
 </widget>
 <widget class="QMenuBar" name="menubar">
 <property name="geometry">
 <rect>
 <x>0</x>
 <y>0</y>
 <width>82</width>
 <height>21</height>
 </rect>
 </property>
 </widget>
 <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</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:

pyside-uic mainwindow.ui -o mainwindow.py

The generated output file "mainwindow.py" is as follows:

#-*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Mon Dec 10 16:22:24 2012
# by: pyside-uic 0.2.13 running on PySide 1.1.0
#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui

class Ui_MainWindow(object):
 def setupUi(self, MainWindow):
 MainWindow.setObjectName("MainWindow")
 MainWindow.resize(82, 64)
 self.centralwidget = QtGui.QWidget(MainWindow)
 self.centralwidget.setObjectName("centralwidget")
 self.gridLayout = QtGui.QGridLayout(self.centralwidget)
 self.gridLayout.setObjectName("gridLayout")
 self.label = QtGui.QLabel(self.centralwidget)
 self.label.setObjectName("label")
 self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
 MainWindow.setCentralWidget(self.centralwidget)
 self.menubar = QtGui.QMenuBar(MainWindow)
 self.menubar.setGeometry(QtCore.QRect(0, 0, 82, 21))
 self.menubar.setObjectName("menubar")
 MainWindow.setMenuBar(self.menubar)
 self.statusbar = QtGui.QStatusBar(MainWindow)
 self.statusbar.setObjectName("statusbar")
 MainWindow.setStatusBar(self.statusbar)

self.retranslateUi(MainWindow)
 QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
 MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
 self.label.setText(QtGui.QApplication.translate("MainWindow", "Hello World!", None, QtGui.QApplication.UnicodeUTF8))

we have to modify this mainwindow.py module:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mainwindow.ui' 
# 
# Created: Mon Dec 10 16:22:24 2012 
# by: pyside-uic 0.2.13 running on PySide 1.1.0 
# 
# WARNING! All changes made in this file will be lost! 

import sys
from PySide import QtCore, QtGui

class Ui_MainWindow(object):
 def setupUi(self, MainWindow):
 MainWindow.setObjectName("MainWindow")
 MainWindow.resize(82, 64)
 self.centralwidget = QtGui.QWidget(MainWindow)
 self.centralwidget.setObjectName("centralwidget")
 self.gridLayout = QtGui.QGridLayout(self.centralwidget)
 self.gridLayout.setObjectName("gridLayout")
 self.label = QtGui.QLabel(self.centralwidget)
 self.label.setObjectName("label")
 self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
 MainWindow.setCentralWidget(self.centralwidget)
 self.menubar = QtGui.QMenuBar(MainWindow)
 self.menubar.setGeometry(QtCore.QRect(0, 0, 82, 21))
 self.menubar.setObjectName("menubar")
 MainWindow.setMenuBar(self.menubar)
 self.statusbar = QtGui.QStatusBar(MainWindow)
 self.statusbar.setObjectName("statusbar")
 MainWindow.setStatusBar(self.statusbar)

self.retranslateUi(MainWindow)
 QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
 MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
 self.label.setText(QtGui.QApplication.translate("MainWindow", "Hello World!", None, QtGui.QApplication.UnicodeUTF8))

class ControlMainWindow(QtGui.QMainWindow):
 def ''init''(self, parent=None):
 super(ControlMainWindow, self).''init''(parent)
 self.ui = Ui_MainWindow()
 self.ui.setupUi(self)

if ''name'' == "''main''":
 app = QtGui.QApplication(sys.argv)
 mySW = ControlMainWindow()
 mySW.show()
 sys.exit(app.exec_())

And to run: python mainwindow.py