博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Differences Between PySide and PyQt
阅读量:6679 次
发布时间:2019-06-25

本文共 5262 字,大约阅读时间需要 17 分钟。

Differences Between PySide and PyQt

API differences

Different import name (PySide instead of PyQt4)

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

 

  1. from PyQt4. import
    *

or

 

  1. import PyQt4.

you have to do the following:

 

  1. from PySide. import
    *

or

  1. import PySide.

.

PySide only supports PyQt’s “API 2” (PSEP 101)

PyQt provides [riverbankcomputing.co.uk], 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 [pyside.org]) 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. [bugs.openbossa.org].

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:

 

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

As described in [pyside.org], 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:

 

  1. .
    Signal
    = .
    pyqtSignal
  2. .
    Slot
    = .
    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 [pyside.org]).

Tool names different

PySide uses different names for the tools scripts:

  • pyuic4 -> pyside-uic
  • pyrcc4 -> pyside-rcc4
  • 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 [doc.qt.nokia.com] and see whether the function has been deprecated and what to use instead.

Example bug: [bugs.openbossa.org]

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 [bugs.openbossa.org] for details.

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

PyQt in some cases allows code such as:

 

  1. class Window
    ( .
    )
    :
  2.     def __init__
    (self
    , parent
    =None
    )
    :
  3.         super
    ( .
    , self
    ).__init__
    (parent
    )
  4.        
    [...
    ]

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

 

  1. class Window
    ( .
    )
    :
  2.     def __init__
    (self
    , parent
    =None
    )
    :
  3.         super
    (Window
    , self
    ).__init__
    (parent
    )
  4.        
    [...
    ]

Old style signals need use of parentheses

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

 

  1. 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:

 

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

You can check the complete discussion on [bugs.pyside.org]

Only signals without arguments can be auto connected on constructor.

If you use:

 

  1. action
    = .
    (None
    , triggered
    =handler
    )

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

Supporting Both APIs

[github.com] 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.

[kforge.ros.org] of the [ros.org] package python_qt_binding is 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

  1. from import

Furthermore it provides a substitute loadUi function for simple loading of ui files. For usage examples see [kforge.ros.org]

Categories:

转载地址:http://wenao.baihongyu.com/

你可能感兴趣的文章
一篇文章带你理解闭包
查看>>
Spring Cloud在国内中小型公司能用起来吗?
查看>>
Java 锁机制 synchronized
查看>>
《中国人工智能学会通讯》——1.33 基础模型
查看>>
Consensus Attention-based Neural Networks for Chinese Reading
查看>>
Angular-个人整理
查看>>
Beten交易所与市场投资者共同发掘数字资产价值
查看>>
MySQL基础语句总结
查看>>
linux 环境变量
查看>>
C#基础知识整理:基础知识(14) 数组
查看>>
Maven多模块项目使用Jenkins分析代码的配置
查看>>
jQery Ajax 执行顺序
查看>>
一篇文章教你看懂Photoshop和Sketch
查看>>
【多图软文】使用Team@OSC进行团队协作
查看>>
阻止文字选中
查看>>
Spring Cloud搭建微服务架构----使用Spring boot开发web项目
查看>>
python 时间格式转化成毫秒
查看>>
java一些需要掌握的知识点
查看>>
CentOS 6.2 yum安装配置lnmp服务器(Nginx+PHP+MySQL)
查看>>
Redis学习手册 比较全面
查看>>