Once people get to know about both PySide and PyQt, due to their similarities, people have a hard time picking one over the other. Here in this “PySide vs PyQt” article we’ll examine both libraries from multiple angles and attempt to come to a conclusion.
The reason why we are comparing these two libraries is because both of them are Python bindings of the same GUI framework Qt. We’re here to compare both these bindings.
We’ll start off with PyQt first as it’s the more poplar library and was released (officially) before PySide.
Background and Licensing
PyQt was created by Riverbank Computing as the Python binding for Qt. For a long time PyQt was the only Python binding available. However in 2009 a dispute arose with regards to what license PyQt should be released under, between the creators of PyQt and the creators of Qt (Nokia).
Since the groups couldn’t come to an agreement, a new Python Binding, PySide was born. PySide (for Qt4) was released under the LGPL license (like Qt), whereas PyQt was released under the GPL license. You can always check out the details on the licenses in more detail later (you really don’t need to) but we’ll give a brief summary here.
The LGPL license allows you to distribute code without having to share your source code. This allows a person to develop commercial applications with PySide. The GPL license however prevents you from withholding the source code in a distributed program.
In short, if you want to develop and distribute commercial programs in PyQt, you should purchase a commercial license for Qt. You could still sell the software for money (without a commercial license), but you would have to share the source code. This is something that’s unacceptable for most commercial software.
Present day
Doing a PyQt vs PySide comparison at the time of the split (2009) would have shown that both were roughly equal. However as the time went on, the development of PySide lagged behind PyQt.
Following the release of Qt5, PyQt5 (the python binding for Qt5) was released in 2016. In contrast, it took PySide an extra 2 years (2018) to release their binding of Qt5 as PySide2.
This two year gap is likely the major reason that PyQt5 is more common amongst the Python developers of today.
Aside from a few syntax differences in how they are imported and launched, the syntax of both libraries is exactly the same. They use the same widgets (Qt widgets) so you can learn about PySide from a PyQt tutorial too, and vice versa. We’ll be covering and explaining these differences down below.
PyQt vs PySide – Differences
In this section we’ll briefly go through the differences between PyQt5 and PySide2, the most latest versions. These differences are going to be pretty minor and very situational (besides the first one) so it’s OK to skip them.
First thing we’ll do is demonstrate how to create a simple window in both PyQt5 and PySide2. You’ll notice how similar how they, as well the slight difference in the imports.
Imports
First up is the PyQt5 example:
import sys from PyQt5.QtWidgets import QApplication, QDialog app = QApplication(sys.argv) window = QDialog() window.setGeometry(500, 300, 300, 200) window.setWindowTitle('GUI Window') window.show() sys.exit(app.exec_())
Next is PySide2:
import sys from PySide2.QtWidgets import QApplication, QDialog app = QApplication(sys.argv) window = QDialog() window.setGeometry(500, 300, 300, 200) window.setWindowTitle('GUI Window') window.show() sys.exit(app.exec_())
We use QDialog
we create the window, but you can use other methods like QMainWindow
and QWidget
, which also work on both PySide2 and PyQt5.
The output of the two examples is also the exact same. Below is a screen shot of it.
Everything related to the widget’s look and functionality is the exact same, so we didn’t bother including any in the window.
UI Files
If you’re loading files exported from QtDesigner into your Python program, there are different functions that must be used for PySide and PyQt.
We’ll let the examples speak for themselves. Below are both the methods listed, first PyQt5 then PySide2.
import sys from PyQt5.QtWidgets import QApplication from PyQt5 import uic app = QApplication(sys.argv) window = uic.loadUi("testfile.ui") window.show() sys.exit(app.exec_())
Now PySide2:
import sys from PySide2.QtWidgets import QApplication from PySide2.QtUiTools import QUiLoader loader = QUiLoader() app = QApplication(sys.argv) window = loader.load("testfile.ui", None) window.show() sys.exit(app.exec_())
Signals and Slots
If you ever want to create your own custom slots or signals in PyQt5 or PySide, keep in mind that the functions are slightly different.
See the below code to see the difference between PyQt5 and PySide when creating a signal or slot object.
my_signal = pyqtSignal() # PyQt5 my_signal = Signal() # PySide2 my_signal = pyqtSlot() # PyQt5 my_signal = Slot() # PySide2
Both of these are part of the QtCore class, so the import is like QtCore.pyqtSignal
or QtCore.Signal
.
If you master all these differences, migrating a PyQt5 app to PySide2 or vice versa will be a piece of cake!
Conclusion
And that’s the end of our PyQt vs PySide debate. Since both of them are bindings of the same library (same widgets and code), there’s no point in comparing the speed and memory usage. Any differences that might be present are going to be insignificant.
If you’re a casual programmer, then the differences in licensing don’t matter at all. The difference will only become significant if you decide to release a commercial software of your own. It costs about $550 to buy a permanent PyQt5 commercial license with a year of support. That’s not too much, especially if the seller is a company or group of developers.
If you had to pick between one, I would just go with PyQt5 due to it’s larger usage and community. Honestly though it’s best to just learn both as the differences are so minimal. You can learn PyQt5 on our tutorial series here on this site.
You aren’t limited to just these two libraries either. There are many other great GUI frameworks in Python, each with their own special advantage.
- Tkinter: Part of the standard Python Library and comes with a very small memory footprint (compared to Qt5). Probably the most popular Python Library due to it’s ease of use.
- Kivy: Another popular GUI framework that specializes in touch screen devices (mobile phones).
This marks the end of the PySide vs PyQt article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.
The post PySide vs PyQt | Understanding the difference appeared first on CodersLegacy.
from Planet Python
via read more
No comments:
Post a Comment