Qt for Python UiFiles: Difference between revisions
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 | 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 />&lt;?xml version="1.0&quot; encoding="UTF-8&quot;?&gt;<br /><ui version="4.0&quot;><br /> <class&gt;MainWindow&lt;/class&gt;<br /> <widget class="QMainWindow&quot; name="MainWindow&quot;><br /> <property name="geometry&quot;><br /> <rect&gt;<br /> <x&gt;0&lt;/x&gt;<br /> <y&gt;0&lt;/y&gt;<br /> <width&gt;82&lt;/width&gt;<br /> <height&gt;64&lt;/height&gt;<br /> </rect&gt;<br /> </property&gt;<br /> <property name="windowTitle&quot;><br /> <string&gt;MainWindow&lt;/string&gt;<br /> </property&gt;<br /> <widget class="QWidget&quot; name="centralwidget&quot;><br /> <layout class="QGridLayout&quot; name="gridLayout&quot;><br /> <item row="0&quot; column="0&quot;><br /> <widget class="QLabel&quot; name="label&quot;><br /> <property name="text&quot;><br /> <string&gt;Hello World!</string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </widget&gt;<br /> <widget class="QMenuBar&quot; name="menubar&quot;><br /> <property name="geometry&quot;><br /> <rect&gt;<br /> <x&gt;0&lt;/x&gt;<br /> <y&gt;0&lt;/y&gt;<br /> <width&gt;82&lt;/width&gt;<br /> <height&gt;21&lt;/height&gt;<br /> </rect&gt;<br /> </property&gt;<br /> </widget&gt;<br /> <widget class="QStatusBar&quot; name="statusbar&quot;/&gt;<br /> </widget&gt;<br /> <resources/&gt;<br /> <connections/&gt;<br /></ui&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 "mainwindow.py&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("MainWindow&quot;)<br /> MainWindow.resize(82, 64)<br /> self.centralwidget = QtGui.QWidget(MainWindow)<br /> self.centralwidget.setObjectName("centralwidget&quot;)<br /> self.gridLayout = QtGui.QGridLayout(self.centralwidget)<br /> self.gridLayout.setObjectName("gridLayout&quot;)<br /> self.label = QtGui.QLabel(self.centralwidget)<br /> self.label.setObjectName("label&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("menubar&quot;)<br /> MainWindow.setMenuBar(self.menubar)<br /> self.statusbar = QtGui.QStatusBar(MainWindow)<br /> self.statusbar.setObjectName("statusbar&quot;)<br /> MainWindow.setStatusBar(self.statusbar) | |||
self.retranslateUi(MainWindow)<br /> QtCore.QMetaObject.connectSlotsByName(MainWindow) | |||
def retranslateUi(self, MainWindow):<br /> MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow&quot;, "MainWindow&quot;, None, QtGui.QApplication.UnicodeUTF8))<br /> self.label.setText(QtGui.QApplication.translate("MainWindow&quot;, "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> | |||
# 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("MainWindow&quot;)<br /> MainWindow.resize(82, 64)<br /> self.centralwidget = QtGui.QWidget(MainWindow)<br /> self.centralwidget.setObjectName("centralwidget&quot;)<br /> self.gridLayout = QtGui.QGridLayout(self.centralwidget)<br /> self.gridLayout.setObjectName("gridLayout&quot;)<br /> self.label = QtGui.QLabel(self.centralwidget)<br /> self.label.setObjectName("label&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("menubar&quot;)<br /> MainWindow.setMenuBar(self.menubar)<br /> self.statusbar = QtGui.QStatusBar(MainWindow)<br /> self.statusbar.setObjectName("statusbar&quot;)<br /> MainWindow.setStatusBar(self.statusbar) | |||
self.retranslateUi(MainWindow)<br /> QtCore.QMetaObject.connectSlotsByName(MainWindow) | |||
def retranslateUi(self, MainWindow):<br /> MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow&quot;, "MainWindow&quot;, None, QtGui.QApplication.UnicodeUTF8))<br /> self.label.setText(QtGui.QApplication.translate("MainWindow&quot;, "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) | |||
== | if ''name'' == "''main''":<br /> app = QtGui.QApplication(sys.argv)<br /> mySW = ControlMainWindow()<br /> mySW.show()<br /> sys.exit(app.exec_())<br /></code> | ||
And to run:<br /><code><br />python mainwindow.py | |||
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" for template. And save as "mainwindow.ui" . Add a Qlabel to the center of the centralwidget. You should get somethig like this :
And your mainwindow.ui
<br />&lt;?xml version="1.0&quot; encoding="UTF-8&quot;?&gt;<br /><ui version="4.0&quot;><br /> <class&gt;MainWindow&lt;/class&gt;<br /> <widget class="QMainWindow&quot; name="MainWindow&quot;><br /> <property name="geometry&quot;><br /> <rect&gt;<br /> <x&gt;0&lt;/x&gt;<br /> <y&gt;0&lt;/y&gt;<br /> <width&gt;82&lt;/width&gt;<br /> <height&gt;64&lt;/height&gt;<br /> </rect&gt;<br /> </property&gt;<br /> <property name="windowTitle&quot;><br /> <string&gt;MainWindow&lt;/string&gt;<br /> </property&gt;<br /> <widget class="QWidget&quot; name="centralwidget&quot;><br /> <layout class="QGridLayout&quot; name="gridLayout&quot;><br /> <item row="0&quot; column="0&quot;><br /> <widget class="QLabel&quot; name="label&quot;><br /> <property name="text&quot;><br /> <string&gt;Hello World!</string&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </item&gt;<br /> </layout&gt;<br /> </widget&gt;<br /> <widget class="QMenuBar&quot; name="menubar&quot;><br /> <property name="geometry&quot;><br /> <rect&gt;<br /> <x&gt;0&lt;/x&gt;<br /> <y&gt;0&lt;/y&gt;<br /> <width&gt;82&lt;/width&gt;<br /> <height&gt;21&lt;/height&gt;<br /> </rect&gt;<br /> </property&gt;<br /> </widget&gt;<br /> <widget class="QStatusBar&quot; name="statusbar&quot;/&gt;<br /> </widget&gt;<br /> <resources/&gt;<br /> <connections/&gt;<br /></ui&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" 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("MainWindow&quot;)<br /> MainWindow.resize(82, 64)<br /> self.centralwidget = QtGui.QWidget(MainWindow)<br /> self.centralwidget.setObjectName("centralwidget&quot;)<br /> self.gridLayout = QtGui.QGridLayout(self.centralwidget)<br /> self.gridLayout.setObjectName("gridLayout&quot;)<br /> self.label = QtGui.QLabel(self.centralwidget)<br /> self.label.setObjectName("label&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("menubar&quot;)<br /> MainWindow.setMenuBar(self.menubar)<br /> self.statusbar = QtGui.QStatusBar(MainWindow)<br /> self.statusbar.setObjectName("statusbar&quot;)<br /> MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)<br /> QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):<br /> MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow&quot;, "MainWindow&quot;, None, QtGui.QApplication.UnicodeUTF8))<br /> self.label.setText(QtGui.QApplication.translate("MainWindow&quot;, "Hello World!", 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("MainWindow&quot;)<br /> MainWindow.resize(82, 64)<br /> self.centralwidget = QtGui.QWidget(MainWindow)<br /> self.centralwidget.setObjectName("centralwidget&quot;)<br /> self.gridLayout = QtGui.QGridLayout(self.centralwidget)<br /> self.gridLayout.setObjectName("gridLayout&quot;)<br /> self.label = QtGui.QLabel(self.centralwidget)<br /> self.label.setObjectName("label&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("menubar&quot;)<br /> MainWindow.setMenuBar(self.menubar)<br /> self.statusbar = QtGui.QStatusBar(MainWindow)<br /> self.statusbar.setObjectName("statusbar&quot;)<br /> MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)<br /> QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):<br /> MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow&quot;, "MainWindow&quot;, None, QtGui.QApplication.UnicodeUTF8))<br /> self.label.setText(QtGui.QApplication.translate("MainWindow&quot;, "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)
if ''name'' == "''main''":<br /> app = QtGui.QApplication(sys.argv)<br /> mySW = ControlMainWindow()<br /> mySW.show()<br /> sys.exit(app.exec_())<br />
And to run:
python mainwindow.py