Migrate PyQt5 app to PySide2
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.
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.
Now, change our code to the following where we namespaced our imports. QMainWindow becomes QtWidgets.QMainWindow and so on –
Converting To Pyside2
Converting our app to PySide2 is as simple as changing our imports to
Caveats to look out for –
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())but PySide2 supports only
- The second thing is signal and slots. Under PyQt5 it’s
QtCore.pyqtSlotand under PySide2 it’s
- The third thing is loading Ui files.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course