Qt for Python UiFiles: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Rename category "LanguageBindings::PySide" -> "PySide")
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:PySide]]
[[Category:Qt for Python]]
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/qt-5/modelview.html Qt Creator Widget Tutorial ].
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)


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.
If you don't know how to use Qt Creator, please go to [http://doc.qt.io/qtcreator/creator-using-qt-designer.html Using Qt Designer].


And your mainwindow.ui
At Qt Creator, create a new Qt Design Form, choose "Main Window" for template.
<code>
And save as "mainwindow.ui".
Add a Qlabel to the center of the centralwidget.
 
Your file (mainwindow.ui) should look something like this:
 
<source lang="xml">
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<ui version="4.0">
Line 49: Line 54:
  <connections/>
  <connections/>
</ui>
</ui>
</code>
</source>
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
 
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):
Now, using PySide2 we will load the ui file and start our application:
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>
 
we have to modify this mainwindow.py module:
 
<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!


<syntaxhighlight lang="python" line='line'>
# main.py
import sys
import sys
from PySide import QtCore, QtGui
from PySide2.QtUiTools import QUiLoader
 
from PySide2.QtWidgets import QApplication
class Ui_MainWindow(object):
from PySide2.QtCore import QFile
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)
if __name__ == "__main__":
QtCore.QMetaObject.connectSlotsByName(MainWindow)
    app = QApplication(sys.argv)


def retranslateUi(self, MainWindow):
    file = QFile("mainwindow.ui")
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
    file.open(QFile.ReadOnly)
self.label.setText(QtGui.QApplication.translate("MainWindow", "Hello World!", None, QtGui.QApplication.UnicodeUTF8))


class ControlMainWindow(QtGui.QMainWindow):
    loader = QUiLoader()
def ''init''(self, parent=None):
    window = loader.load(file)
super(ControlMainWindow, self).''init''(parent)
    window.show()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)


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


And to run: <code>python mainwindow.py</code>
And to run it, just a <code>python main.py</code> will do the job.

Latest revision as of 15:17, 27 May 2018


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

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")
    file.open(QFile.ReadOnly)

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

    sys.exit(app.exec_())

And to run it, just a

python main.py

will do the job.