Qt for Python UiFiles: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
 
(15 intermediate revisions by 6 users not shown)
Line 1: Line 1:
=How to use Qt Creator to design Graphical Interfaces for 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).
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 [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 [http://doc.qt.io/qtcreator/creator-using-qt-designer.html 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. 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.


[[Image:mainwindow.png|mainwindow]]
Your file (mainwindow.ui) should look something like this:


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 />
<source lang="xml">
<?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>
</source>


The generated output file “mainwindow.py” is as follows:<br /> we have to modify this mainwindow.py module:<br />
Now, using PySide2 we will load the ui file and start our application:


And to run:<br />
<syntaxhighlight lang="python" line='line'>
# main.py
import sys
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import QFile


===Categories:===
if __name__ == "__main__":
    app = QApplication(sys.argv)


* [[:Category:LanguageBindings|LanguageBindings]]
    file = QFile("mainwindow.ui")
** [[:Category:LanguageBindings::PySide|PySide]]
    file.open(QFile.ReadOnly)
 
    loader = QUiLoader()
    window = loader.load(file)
    window.show()
 
    sys.exit(app.exec_())
</syntaxhighlight>
 
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.