Differences Between PySide and PyQt: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
No edit summary
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}


[[Category:LanguageBindings::PySide]]
[[Category:PySide]]
 
{| class="wikitable"
|-
| style="background: #ff6961;text-align: center;"| Attention
|-
| This is a page dedicated to PySide (Qt4). For recent development on PySide2 (Qt5) and PySide6 (Qt6) refer to [[Qt for Python]]
|}
 
'''English''' [[Differences Between PySide and PyQt SimplifiedChinese|简体中文]]
'''English''' [[Differences Between PySide and PyQt SimplifiedChinese|简体中文]]
[toc align_right="yes" depth="3"]


= Differences Between PySide and PyQt =
 


== API differences ==
== API differences ==
Line 36: Line 42:
=== PySide only supports PyQt's "API 2" (PSEP 101) ===
=== PySide only supports PyQt's "API 2" (PSEP 101) ===


PyQt provides "two different APIs":http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#selecting-incompatible-apis, the first of which provides <code>QStrings</code>, <code>QVariants</code>, etc as is in Python. The new API 2 provides automatic conversion between the Qt classes and respective native Python datatypes and is much more Pythonic in nature. PyQt on Python 2.x defaults to API 1, while PyQt on Python 3 defaults to API 2.
PyQt provides [http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#selecting-incompatible-apis two different APIs], the first of which provides QStrings, QVariants, etc as is in Python. The new API 2 provides automatic conversion between the Qt classes and respective native Python datatypes and is much more Pythonic in nature. PyQt on Python 2.x defaults to API 1, while PyQt on Python 3 defaults to API 2.


PySide only supports PyQt's API 2 (see "PSEP 101":http://www.pyside.org/docs/pseps/psep-0101.html) for details. Therefore Qt classes such as <code>QStrings</code>, <code>QStringLists</code>, and <code>QVariants</code> are not available on PySide. Instead, you should simply use native Python datatypes.
PySide only supports PyQt's API 2 (see [http://www.pyside.org/docs/pseps/psep-0101.html PSEP 101]) for details. Therefore Qt classes such as QStrings, QStringLists, and QVariants are not available on PySide. Instead, you should simply use native Python datatypes.


If you're porting code from PyQt, you might want to first modify the PyQt code to use API 2 (using a <code>sip.setapi(class,ver)</code> call before importing PyQt4), and only after getting that change working, change the imports to use PySide instead.
If you're porting code from PyQt, you might want to first modify the PyQt code to use API 2 (using a sip.setapi(class,ver) call before importing PyQt4), and only after getting that change working, change the imports to use PySide instead.


'''NB:''' Due to the API change, <code>QFileDialog.getOpenFileName</code> returns a tuple in PySide, which often is an issue when porting code from PyQt. See e.g. "bug 343":http://bugs.openbossa.org/show_bug.cgi?id=343.
'''NB:''' Due to the API change, QFileDialog.getOpenFileName returns a tuple in PySide, which often is an issue when porting code from PyQt. See e.g. [http://bugs.openbossa.org/show_bug.cgi?id=343 bug 343].


=== New-style signals and slots use slightly different syntax (PSEP 100) ===
=== New-style signals and slots use slightly different syntax (PSEP 100) ===
Line 53: Line 59:
</code>
</code>


As described in "PSEP 100":http://www.pyside.org/docs/pseps/psep-0100.html, use <code>QtCore.Signal()</code> and <code>QtCore.Slot()</code> instead.
As described in [http://www.pyside.org/docs/pseps/psep-0100.html PSEP 100], use QtCore.Signal() and QtCore.Slot() instead.


If you want to modify your PyQt code to use the PySide naming scheme, that can be done using a simple definition:
If you want to modify your PyQt code to use the PySide naming scheme, that can be done using a simple definition:
Line 65: Line 71:
=== Declaring Qt Properties is done using a slightly different syntax (PSEP 103) ===
=== Declaring Qt Properties is done using a slightly different syntax (PSEP 103) ===


As with the signal/slot syntax change above, declaring Qt Properties is done using <code>QtCore</code>. Property instead of <code>QtCore.pyqtProperty</code> (see "PSEP 103":http://www.pyside.org/docs/pseps/psep-0103.html).
As with the signal/slot syntax change above, declaring Qt Properties is done using QtCore. Property instead of QtCore.pyqtProperty (see [http://www.pyside.org/docs/pseps/psep-0103.html PSEP 103]).


=== Tool names different ===
=== Tool names different ===
Line 77: Line 83:
=== Property Names ===
=== Property Names ===


PySide uses <code>connect</code> and <code>event</code> in the <code>QObject</code>. Do not use these for anything in your code, and when moving code from PyQt look for any that exist.
PySide uses connect and event in the QObject. Do not use these for anything in your code, and when moving code from PyQt look for any that exist.


=== QThreads ===
=== QThreads ===


In PySide you should <code>.wait()</code> on a thread after calling <code>.stop()</code> if you are quitting the application. Otherwise you may receive
In PySide you should .wait() on a thread after calling .stop() if you are quitting the application. Otherwise you may receive


<code>QThread: Destroyed while thread is still running
QThread: Destroyed while thread is still running
Segmentation fault</code>
Segmentation fault


== Differences in the functionality of the bindings ==
== Differences in the functionality of the bindings ==
Line 92: Line 98:
Bindings for functions deprecated before Qt 4.5 are not generated in PySide.
Bindings for functions deprecated before Qt 4.5 are not generated in PySide.


If a function is not present in PySide, check the "Qt Online Reference Documentation":http://doc.qt.nokia.com/ and see whether the function has been deprecated and what to use instead.
If a function is not present in PySide, check the [http://doc.qt.io/ Qt Online Reference Documentation] and see whether the function has been deprecated and what to use instead.


Example bug: "bug 359":http://bugs.openbossa.org/show_bug.cgi?id=359
Example bug: [http://bugs.openbossa.org/show_bug.cgi?id=359 bug 359]


For example, this affects functions such as <code>QColor.dark()</code> and <code>QColor.light()</code>, instead of which <code>QColor.darker()</code> and <code>QColor.lighter()</code> need to be used.
For example, this affects functions such as QColor.dark() and QColor.light(), instead of which QColor.darker() and QColor.lighter() need to be used.


=== sender() method returns None when used within a partial or a lambda ===
=== sender() method returns None when used within a partial or a lambda ===


If a lambda is used as a slot, <code>sender()</code> cannot be used to acquire the object that emitted the signal. This works in PyQt, but their implementation behaves incorrectly in certain situations. See "bug 344":http://bugs.openbossa.org/show_bug.cgi?id=344 for details.
If a lambda is used as a slot, sender() cannot be used to acquire the object that emitted the signal. This works in PyQt, but their implementation behaves incorrectly in certain situations. See [http://bugs.openbossa.org/show_bug.cgi?id=344 bug 344] for details.


=== When inheriting classes, parent class constructors need to be always called ===
=== When inheriting classes, parent class constructors need to be always called ===
Line 127: Line 133:


<code>
<code>
self.emit(SIGNAL (&amp;#39;text_changed_cb&amp;#39;), text)
self.emit(SIGNAL ('text_changed_cb'), text)
</code>
</code>


Line 133: Line 139:


<code>
<code>
self.emit(SIGNAL (&amp;#39;text_changed_cb(QString)'), text)
self.emit(SIGNAL ('text_changed_cb(QString)'), text)
</code>
</code>


You can check the complete discussion on "bug #314":http://bugs.pyside.org/show_bug.cgi?id=314
You can check the complete discussion on [http://bugs.pyside.org/show_bug.cgi?id=314 bug #314]


=== Only signals without arguments can be auto connected on constructor. ===
=== Only signals without arguments can be auto connected on constructor. ===
Line 150: Line 156:
= Supporting Both APIs =
= Supporting Both APIs =


"qt_compat.py":https://github.com/epage/PythonUtils/blob/master/util/qt_compat.py is an example of centralizing the knowledge of PySide and PyQt without using monkey-patching. It serves both to point out what differences exist as well as keeping your code binding-agnostic. This is important for when needing to support both bindings, usually when transitioning from one to the other and needing to continue to support older distributions that do not yet come with PySide.
[https://github.com/epage/PythonUtils/blob/master/util/qt_compat.py qt_compat.py] is an example of centralizing the knowledge of PySide and PyQt without using monkey-patching. It serves both to point out what differences exist as well as keeping your code binding-agnostic. This is important for when needing to support both bindings, usually when transitioning from one to the other and needing to continue to support older distributions that do not yet come with PySide.


"python_qt_binding":https://github.com/ros-visualization/python_qt_binding is a Python package from the "ROS":http://www.ros.org middleware which gives a more extensive example of how to keep your code agnostic of the actual Python-Qt binding used. It offers a very transparent abstraction, allowing you to write standard import lines like <code>from python_qt_binding.QtCore import QObject<code> Furthermore it provides a substitute '''loadUi''' function for simple loading of ui files that work the same with both bindings. For usage examples see "rqt_gui":https://github.com/ros-visualization/rqt
[https://github.com/ros-visualization/python_qt_binding python_qt_binding] is a Python package from the [http://www.ros.org ROS] middleware which gives a more extensive example of how to keep your code agnostic of the actual Python-Qt binding used. It offers a very transparent abstraction, allowing you to write standard import lines like from python_qt_binding.QtCore import QObject Furthermore it provides a substitute '''loadUi''' function for simple loading of ui files that work the same with both bindings. For usage examples see [https://github.com/ros-visualization/rqt rqt_gui]


= Converting PyQt4 code to PySide =
= Converting PyQt4 code to PySide =

Latest revision as of 10:33, 24 February 2022


Attention
This is a page dedicated to PySide (Qt4). For recent development on PySide2 (Qt5) and PySide6 (Qt6) refer to Qt for Python

English 简体中文


API differences

Different import name (PySide instead of PyQt4)

PySide uses a different library name than PyQt. Instead of typing:

from PyQt4.QtCore import *

or

import PyQt4.QtCore

you have to do the following:

from PySide.QtCore import *

or

import PySide.QtCore

.

PySide only supports PyQt's "API 2" (PSEP 101)

PyQt provides two different APIs, the first of which provides QStrings, QVariants, etc as is in Python. The new API 2 provides automatic conversion between the Qt classes and respective native Python datatypes and is much more Pythonic in nature. PyQt on Python 2.x defaults to API 1, while PyQt on Python 3 defaults to API 2.

PySide only supports PyQt's API 2 (see PSEP 101) for details. Therefore Qt classes such as QStrings, QStringLists, and QVariants are not available on PySide. Instead, you should simply use native Python datatypes.

If you're porting code from PyQt, you might want to first modify the PyQt code to use API 2 (using a sip.setapi(class,ver) call before importing PyQt4), and only after getting that change working, change the imports to use PySide instead.

NB: Due to the API change, QFileDialog.getOpenFileName returns a tuple in PySide, which often is an issue when porting code from PyQt. See e.g. bug 343.

New-style signals and slots use slightly different syntax (PSEP 100)

PyQt unfortunately uses an implementation-specific naming scheme for its new-style signal and slot classes:

# Define a new signal called 'trigger' that has no arguments.
trigger = QtCore.pyqtSignal()

As described in PSEP 100, use QtCore.Signal() and QtCore.Slot() instead.

If you want to modify your PyQt code to use the PySide naming scheme, that can be done using a simple definition:

QtCore.Signal = QtCore.pyqtSignal
QtCore.Slot = QtCore.pyqtSlot

.

Declaring Qt Properties is done using a slightly different syntax (PSEP 103)

As with the signal/slot syntax change above, declaring Qt Properties is done using QtCore. Property instead of QtCore.pyqtProperty (see PSEP 103).

Tool names different

PySide uses different names for the tools scripts:

  • pyuic4 -> pyside-uic
  • pyrcc4-> pyside-rcc
  • pylupdate4 -> pyside-lupdate

Property Names

PySide uses connect and event in the QObject. Do not use these for anything in your code, and when moving code from PyQt look for any that exist.

QThreads

In PySide you should .wait() on a thread after calling .stop() if you are quitting the application. Otherwise you may receive

QThread: Destroyed while thread is still running Segmentation fault

Differences in the functionality of the bindings

Bindings for functions deprecated before Qt 4.5 are not generated

Bindings for functions deprecated before Qt 4.5 are not generated in PySide.

If a function is not present in PySide, check the Qt Online Reference Documentation and see whether the function has been deprecated and what to use instead.

Example bug: bug 359

For example, this affects functions such as QColor.dark() and QColor.light(), instead of which QColor.darker() and QColor.lighter() need to be used.

sender() method returns None when used within a partial or a lambda

If a lambda is used as a slot, sender() cannot be used to acquire the object that emitted the signal. This works in PyQt, but their implementation behaves incorrectly in certain situations. See bug 344 for details.

When inheriting classes, parent class constructors need to be always called

PyQt in some cases allows code such as:

class Window(QtGui.QWidget):
 def ''init''(self, parent=None):
 super(QtGui.QWidget, self).''init''(parent)
 []

in which the direct parent class constructor is not called. PySide expects you to do the right thing:

class Window(QtGui.QWidget):
 def ''init''(self, parent=None):
 super(Window, self).''init''(parent)
 []

Old style signals need use of parentheses

PyQt allows the use of short-circuit signals without parentheses such as:

self.emit(SIGNAL ('text_changed_cb'), text)

Since this is an old and deprecated feature, and the effort to fix this is not worth it, we decided to not implement it. In PySide code you need to use something like:

self.emit(SIGNAL ('text_changed_cb(QString)'), text)

You can check the complete discussion on bug #314

Only signals without arguments can be auto connected on constructor.

If you use:

action = QtGui.QAction(None, triggered=handler)

The triggered() signal will be connected to the slot handler instead of the triggered(bool) signal.

Supporting Both APIs

qt_compat.py is an example of centralizing the knowledge of PySide and PyQt without using monkey-patching. It serves both to point out what differences exist as well as keeping your code binding-agnostic. This is important for when needing to support both bindings, usually when transitioning from one to the other and needing to continue to support older distributions that do not yet come with PySide.

python_qt_binding is a Python package from the ROS middleware which gives a more extensive example of how to keep your code agnostic of the actual Python-Qt binding used. It offers a very transparent abstraction, allowing you to write standard import lines like from python_qt_binding.QtCore import QObject Furthermore it provides a substitute loadUi function for simple loading of ui files that work the same with both bindings. For usage examples see rqt_gui

Converting PyQt4 code to PySide