There are two main methods for developing GUI application with qt:
1) Adding all widgets in your code (your cpp or python code)
2) Creating qt UI files, adding widgets there and load everything into your application.
1)Adding all widgets in your code
Here is the snippet for adding all widgets and their slots in code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import sys print(sys.version) import warnings warnings.filterwarnings('ignore') from PyQt5 import QtCore, QtWidgets from PyQt5.QtWidgets import QMainWindow, QLabel, QSlider from PyQt5.QtCore import QSize, Qt, pyqtSlot,pyqtSignal class HelloWindow(QMainWindow): pushButtonClicked = pyqtSignal() valueUpdating = pyqtSignal(int) def pushButtonClicked(self): self.title.setText("PyQt updated!") def valueUpdating(self, value): self.title.setText(str(value)) def __init__(self): QMainWindow.__init__(self) self.setMinimumSize(QSize(640, 480)) self.setWindowTitle("Hello world") self.title = QLabel("PyQt!", self) self.title.setAlignment(QtCore.Qt.AlignCenter) self.pushButton = QtWidgets.QPushButton(self) self.pushButton.setGeometry(QtCore.QRect(140, 200, 99, 27))#x,y, width, height self.pushButton.setObjectName("pushButton") self.pushButton.setText("Click Me!") self.pushButton.clicked.connect(self.pushButtonClicked) self.slider = QtWidgets.QSlider(self) self.slider.setGeometry(QtCore.QRect(100, 20, 100, 30)) self.slider.setFocusPolicy(Qt.StrongFocus) self.slider.setTickPosition(QSlider.TicksBothSides) self.slider.setSingleStep(1) self.slider.setPageStep(1) self.slider.setMaximum(100) self.slider.setMinimum(0) self.slider.setOrientation(Qt.Horizontal) self.slider.valueChanged.connect(self.valueUpdating) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) mainWin = HelloWindow() mainWin.show() sys.exit( app.exec_() ) |
2) Creating qt UI files, adding widgets there and load everything into your application
Now let’s do what we have done in the first method in a UI file and load it. First, create a text file and put the followings in it and save it as “mainwindow.ui”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<?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>617</width> <height>419</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QPushButton" name="pushButton"> <property name="geometry"> <rect> <x>200</x> <y>120</y> <width>99</width> <height>27</height> </rect> </property> <property name="text"> <string>PushButton</string> </property> </widget> <widget class="QLabel" name="title"> <property name="geometry"> <rect> <x>60</x> <y>30</y> <width>67</width> <height>17</height> </rect> </property> <property name="text"> <string>PyQt!</string> </property> </widget> <widget class="QSlider" name="slider"> <property name="geometry"> <rect> <x>190</x> <y>30</y> <width>160</width> <height>29</height> </rect> </property> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> </widget> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>617</width> <height>25</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/> </ui> |
Now call it in your python file like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import sys print(sys.version) import warnings warnings.filterwarnings('ignore') from PyQt5 import QtCore, QtWidgets from PyQt5.QtWidgets import QMainWindow, QLabel, QSlider from PyQt5.QtCore import QSize, Qt, pyqtSlot,pyqtSignal from PyQt5.uic import loadUi class HelloWindow(QMainWindow): pushButtonClicked = pyqtSignal() valueUpdating = pyqtSignal(int) def pushButtonClicked(self): self.title.setText("PyQt updated!") def valueUpdating(self, value): self.title.setText(str(value)) def __init__(self): QMainWindow.__init__(self) self.setMinimumSize(QSize(640, 480)) self.setWindowTitle("Hello world") loadUi('mainwindow.ui',self) self.pushButton.clicked.connect(self.pushButtonClicked) self.slider.valueChanged.connect(self.valueUpdating) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) mainWin = HelloWindow() mainWin.show() sys.exit( app.exec_() ) |
The results should be the same as what you got in the first method.