[PyQt5] PyQt5與PyQt4的差別

原文鏈接:https://blog.csdn.net/qw351363619/article/details/72301803

描述能力有限,不確定的翻譯會在旁邊注釋英文
更新時間:2017年05月12日
官網(wǎng)鏈接

不與 PyQt4 兼容

雖然實際上升級PyQt4寫的項目不是那么糟

不再對Python老版本提供支持(Python 2.6 之前)

不再實現(xiàn)PyQt4不推薦的API接口

PyQt5 不支持任何在PyQt4版本中標(biāo)記為不推薦或舍棄的Qt API(如果有就會當(dāng)Bug處理)

不再提供多版本API接口

PyQt4 支持多版本的API(如QString,QVariant等)
PyQt5 只支持最新的API版本(除QVariant外)
QVariant的改變是去掉了 QPyNullVariant (在QVariant的幫助文檔里也有顯示)

信號和插槽(Signals and Slots)機制更新

# 下面所列出來的調(diào)用方式不再支持
QObject.connect()
QObject.emit()
SIGNAL()
SLOT()

所有含有以SIGNAL()或SLOT()返回結(jié)果為參數(shù)的方法不再支持,轉(zhuǎn)而提供可調(diào)用方法(函數(shù))或已捆綁的信號(a bound signal)

風(fēng)格對比(代碼)
# PyQt5
combo = QtWidgets.QComboBox(self)
combo.activated.connect(self.onActivated)
# PyQt4
combo = QtWidgets.QComboBox(self)
self.connect(combo, QtCore.pyqtSignal('activated(QString)'), self.onActivated)
QObject.disconnect() 調(diào)用無參數(shù),作用斷掉所有信號和插槽的連接
TODO New-style Signals and Slots

Qt implements signals with an optional argument as two separate signals, one with the argument and one without it. PyQt4 exposed both of these allowing you to connect to each of them. However, when emitting the signal, you had to use the signal appropriate to the number of arguments being emitted.

PyQt5 exposes only the signal where all arguments are specified. However it allows any optional arguments to be omitted when emitting the signal.

Unlike PyQt4, PyQt5 supports the definition of properties, signals and slots in classes not sub-classed from QObject (i.e. in mixins).

新增 QtQml QtQuick 模塊并支持從QML創(chuàng)建Python對象

不再支持QtDeclarative, QtScript, QtScriptTools模塊
以上模塊被 QtQmlQtQuick 替換。
支持從QML創(chuàng)建Python對象

QtGui 模塊更新

QtGui模塊被拆分了為QtGui, QtPrintSupport 和QtWidgets三大模塊

from PyQt5 import QtGui, QtPrintSupport, QtWidgets

QtOpenGL 模塊更新

PyQt5的QtOpenGL模塊只提供QGLContext QGLFormatQGLWidget

QtWebKit 模塊更新

PyQt4的QtWebKit在PyQt5中分成了QtWebKitQtWebKitWidgets模塊

擴展性增強

不再支持pyqtconfig模塊
The PyQt5 Extension API
PyQt5 支持第三方包直接基于PyQt5開發(fā)(如QScintilla)

dbus.mainloop.qt 模塊更名

#dbus.mainloop.qt
dbus.mainloop.pyqt5 # 相同功能只更名

QDataStream 明顯數(shù)值的參數(shù)以數(shù)值處理和返回

readUint8(); readInt8(); writeUInt8(); writeInt8() 方法在PyQt5中以數(shù)值類型寫入和返回(PyQt4中是以數(shù)值文本)

QFileDialog 文件操作接口更新

PyQt5 PyQt4 備注
getOpenFileName() getOpenFileNameAndFilter()
getOpenFileNames() getOpenFileNamesAndFilter()
getSaveFileNameAndFilter() getSaveFileName()

PyQt5 舍棄了 PyQt4 同名的方法

QMatrix 方法不再支持

PyQt5 中已經(jīng)不再支持 PyQt4種不推薦方法 QMatrix
PyQt5 中可以考慮使用 QPropertyAnimation

QGraphicsItemAnimation 方法不再支持

PyQt5 中已經(jīng)不再支持 PyQt4種不推薦方法 QGraphicsItemAnimation
PyQt5 中可以考慮使用 QTransform

QPyTextObject 被舍棄

PyQt4 implements the QPyTextObject as a workaround for the inability to define a Python class that is sub-classed from more than one Qt class. PyQt5 does support the ability to define a Python class that is sub-classed from more than one Qt class so long as all but one of the Qt classes are interfaces, i.e. they have been declared in C++ as such using Q_DECLARE_INTERFACE. Therefore QPyTextObject is not implemented in PyQt5.

QSet 在PyQt5中完全用 集合 形式實現(xiàn)

pyuic5不再支持–pyqt3-wrapper參數(shù)

pyuic5 does not support the –pyqt3-wrapper flag of pyuic4.

pyrcc5 不再支持 -py2 或-py3參數(shù)

pyrcc5 does not support the -py2 and -py3 flags of pyrcc4. The output of pyrcc5 is compatible with all versions of Python starting with Python v2.6.

PyQt5 優(yōu)化 合作性多重繼承 (Cooperative Multi-inheritance)的初始化方式

# PyQt5 調(diào)用父類的`__init__`方法.
def __init(self, **kwargs):
    super().__init__(**kwargs)

PyQt5 自動釋放GIL,而不是PyQt4的強制釋放

PyQt5 退出時自動調(diào)用sip.setdestroyonexit()以禁用自動析構(gòu)

Python解釋器退出PyQt4應(yīng)用程序時會默認(rèn)調(diào)用C++析構(gòu)器處理所有它擁有的線程(這通常是以隨機的順序,因此可能會導(dǎo)致解析器崩潰),通過調(diào)用 sip.setdestroyonexit() 函數(shù)可以禁用。
PyQt5 總會自動調(diào)用 sip.setdestroyonexit() 函數(shù).

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,847評論 0 10
  • 本篇文章是基于谷歌有關(guān)Graphic的一篇概覽文章的翻譯:http://source.android.com/de...
    lee_3do閱讀 7,451評論 2 21
  • 孫誠澤來到圖書館,并沒有去攻擊性書籍室,而是拐彎進(jìn)醫(yī)療書籍室,這里布局跟攻擊性書籍室大同小異,幾乎一模一樣。 這個...
    S真龍閱讀 545評論 0 2
  • 1 “呼呼”,“呼呼”,雖說是已經(jīng)是下午兩三點,坡上的風(fēng)力可是一點都沒有減弱,像拔河似的,一下一下拽著阿賢手中的線...
    胖牛兒閱讀 1,882評論 36 38
  • 隨筆7——貼標(biāo)簽 貼標(biāo)簽大家都會,但這里寫的貼標(biāo)簽,可不是現(xiàn)實中的標(biāo)簽,而是貼在心中...
    若水ct十陳濤閱讀 449評論 1 6

友情鏈接更多精彩內(nèi)容