Qt for Python UiFiles: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=How to use Qt Creator to design Graphical Interfaces for PySide=
[[Category:LanguageBindings::PySide]]
 
= 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).
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 [http://doc.qt.io/qtcreator-2.5/creator-writing-program.html Qt Creator (ver 2.5) Widget Tutorial] ''[qt.io]''.
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 :
 
[[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>
<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!
 
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)
 
self.retranslateUi(MainWindow)<br /> 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))
 
</code><br />we have to modify this mainwindow.py module:<br /><code><br /># <s>*</s> coding: utf-8 <s>*</s>
 
# 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!


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 :
import sys<br />from PySide import QtCore, QtGui


[[Image:mainwindow.png|mainwindow]]
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)


And your mainwindow.ui <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 />
self.retranslateUi(MainWindow)<br /> QtCore.QMetaObject.connectSlotsByName(MainWindow)


The generated output file “mainwindow.py” is as follows:<br /> we have to modify this mainwindow.py module:<br />
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))


And to run:<br />
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)


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


* [[:Category:LanguageBindings|LanguageBindings]]
And to run:<br /><code><br />python mainwindow.py
** [[:Category:LanguageBindings::PySide|PySide]]

Revision as of 14:14, 23 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&quot; for template. And save as "mainwindow.ui&quot; . Add a Qlabel to the center of the centralwidget. You should get somethig like this :

mainwindow

And your mainwindow.ui

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


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:

<br />pyside-uic mainwindow.ui <s>o mainwindow.py<br />


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

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

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)

self.retranslateUi(MainWindow)<br /> 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))


we have to modify this mainwindow.py module:

<br /># <s>*</s> coding: utf-8 <s>*</s>

# 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<br />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)

self.retranslateUi(MainWindow)<br /> 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))

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)

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

And to run:

python mainwindow.py