Migrate PyQt5 app to PySide2 - GeeksforGeeks

27 Feb, 2020
One of the most advanced packages for Gui development in Python is PyQt5. According to Christian Tismer, the maintainer of Pyside2, PyQt5 has some 25,000 functions for you to use. That’s a really big library. However, if you want to distribute your app commercially, you need to purchase a license from the Qt company. Fortunately enough, PySide2 allows you the same liberty as Free/Libre Software.
Let’s see how we can migrate a typical PyQt5 app to PySide2.
import sysfrom PyQt5.QtWidgets import (QGridLayout)from PyQt5.QtWidgets import (QMainWindow)from PyQt5.QtWidgets import (QApplication)from PyQt5.QtWidgets import (                                QWidget,                                QPushButton,                                QLabel,                                QLineEdit                            )  from PyQt5.QtCore import Qtfrom PyQt5.QtGui import (QPixmap,QIcon)    class Window(QMainWindow):      def __init__(self, parent = None):        super().__init__(parent)        self.setWindowTitle('PyQt5 Demo App')        self.initGui()        def initGui(self):        self.layout = QGridLayout()        self.window = QWidget()        self.window.setLayout(self.layout)        self.setCentralWidget(self.window)          self.num1_label = QLabel('Enter first number:')        self.num1_label.setAlignment(Qt.AlignCenter)        self.text_box1 = QLineEdit()        self.num2_label = QLabel('Enter second number:')        self.text_box2 = QLineEdit()                  self.get_answer = QPushButton()        calculate_icon = QPixmap('path_to_image.png')        self.get_answer.setIcon(QIcon(calculate_icon))                  self.answer_label = QLabel('---')          self.layout.addWidget(self.num1_label, 0, 0)        self.layout.addWidget(self.text_box1, 1, 0)        self.layout.addWidget(self.num2_label, 2, 0)        self.layout.addWidget(self.text_box2, 3, 0)        self.layout.addWidget(self.get_answer, 4, 0)        self.layout.addWidget(self.answer_label, 5, 0)    if __name__ == '__main__':    app = QApplication(sys.argv)      win = Window()    win.show()      sys.exit(app.exec_())
Output:
We’ve deliberately made use of an example that uses QtWidget, QtCore and QtGui. One thing to do is to take on good practice. it about avoiding from X import *. Namespacing our imports has the advantage of learning what falls under what module.
import sys    from PyQt5 import QtWidgetsfrom PyQt5 import QtCorefrom PyQt5 import QtGui
Now, change our code to the following where we namespaced our imports. QMainWindow becomes QtWidgets.QMainWindow and so on –
class Window(QtWidgets.QMainWindow):      def __init__(self, parent = None):        super().__init__(parent)        self.setWindowTitle('PyQt5 Demo App')        self.initGui()        def initGui(self):        self.layout = QtWidgets.QGridLayout()        self.window = QtWidgets.QWidget()        self.window.setLayout(self.layout)        self.setCentralWidget(self.window)          self.num1_label = QtWidgets.QLabel('Enter first number:')        self.num1_label.setAlignment(QtCore.Qt.AlignCenter)        self.text_box1 = QtWidgets.QLineEdit()        self.num2_label = QtWidgets.QLabel('Enter second number:')        self.text_box2 = QtWidgets.QLineEdit()                  self.get_answer = QtWidgets.QPushButton()        calculate_icon = QtGui.QPixmap('path_to_image.png')        self.get_answer.setIcon(QtGui.QIcon(calculate_icon))                  self.answer_label = QtWidgets.QLabel('---')          self.layout.addWidget(self.num1_label, 0, 0)        self.layout.addWidget(self.text_box1, 1, 0)        self.layout.addWidget(self.num2_label, 2, 0)        self.layout.addWidget(self.text_box2, 3, 0)        self.layout.addWidget(self.get_answer, 4, 0)        self.layout.addWidget(self.answer_label, 5, 0)    if __name__ == '__main__':    app = QtWidgets.QApplication(sys.argv)      win = Window()    win.show()      sys.exit(app.exec_())
Converting To Pyside2
Converting our app to PySide2 is as simple as changing our imports to
import sys    from PySide2 import QtWidgetsfrom PySide2 import QtCorefrom PySide2 import QtGui
For most uses, migrating means adjusting imports, defining functions and using supported funcions only.
One thing to look out is app.exec_. exec_ was used as exec is a Python2 keyword. Under Python3, PyQt5 allows the use of exec but not PySide2. PyQt5 supports both sys.exit(app.exec_()) and sys.exit(app.exec()) but PySide2 supports only app.exec_().The second thing is signal and slots. Under PyQt5 it’s QtCore.pyqtSignal and QtCore.pyqtSlot and under PySide2 it’s QtCore.Signal and QtCore.Slot .The third thing is loading Ui files.
One thing to look out is app.exec_. exec_ was used as exec is a Python2 keyword. Under Python3, PyQt5 allows the use of exec but not PySide2. PyQt5 supports both sys.exit(app.exec_()) and sys.exit(app.exec()) but PySide2 supports only app.exec_().
The second thing is signal and slots. Under PyQt5 it’s QtCore.pyqtSignal and QtCore.pyqtSlot and under PySide2 it’s QtCore.Signal and QtCore.Slot .
The third thing is loading Ui files.
Python-gui
Python-PyQt
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Reading and Writing to text files in Python
sum() function in Python
Create a Pandas DataFrame from Lists