天天看點

pyqt與python的差別_[PyQt5] PyQt5與PyQt4的差别

描述能力有限,不确定的翻譯會在旁邊注釋英文

更新時間:2017年05月12日

官網連結

不與 PyQt4 相容

雖然實際上更新PyQt4寫的項目不是那麼糟

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

不再實作PyQt4不推薦的API接口

PyQt5 不支援任何在PyQt4版本中标記為不推薦或舍棄的Qt API(如果有就會當Bug處理)

不再提供多版本API接口

PyQt4 支援多版本的API(如QString,QVariant等)

PyQt5 隻支援最新的API版本(除QVariant外)

QVariant的改變是去掉了 QPyNullVariant (在QVariant的幫助文檔裡也有顯示)

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

# 下面所列出來的調用方式不再支援

QObject.connect()

QObject.emit()

SIGNAL()

SLOT()

所有含有以SIGNAL()或SLOT()傳回結果為參數的方法不再支援,轉而提供可調用方法(函數)或已捆綁的信号(a bound signal)

風格對比(代碼)

# 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() 調用無參數,作用斷掉所有信号和插槽的連接配接

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建立Python對象

不再支援QtDeclarative, QtScript, QtScriptTools子產品

以上子產品被 QtQml 和 QtQuick 替換。

支援從QML建立Python對象

QtGui 子產品更新

QtGui子產品被拆分了為QtGui, QtPrintSupport 和QtWidgets三大子產品

from PyQt5 import QtGui, QtPrintSupport, QtWidgets

QtOpenGL 子產品更新

PyQt5的QtOpenGL子產品隻提供QGLContext QGLFormat 和 QGLWidget類

QtWebKit 子產品更新

PyQt4的QtWebKit在PyQt5中分成了QtWebKit和QtWebKitWidgets子產品

擴充性增強

不再支援pyqtconfig子產品

The PyQt5 Extension API

PyQt5 支援第三方包直接基于PyQt5開發(如QScintilla)

dbus.mainloop.qt 子產品更名

#dbus.mainloop.qt

dbus.mainloop.pyqt5 # 相同功能隻更名

QDataStream 明顯數值的參數以數值處理和傳回

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

QFileDialog 檔案操作接口更新

PyQt5

PyQt4

備注

getOpenFileName()

getOpenFileNameAndFilter()

getOpenFileNames()

getOpenFileNamesAndFilter()

getSaveFileNameAndFilter()

getSaveFileName()

PyQt5 舍棄了 PyQt4 同名的方法

QMatrix 方法不再支援

PyQt5 中已經不再支援 PyQt4種不推薦方法 QMatrix

PyQt5 中可以考慮使用 QPropertyAnimation

QGraphicsItemAnimation 方法不再支援

PyQt5 中已經不再支援 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中完全用 集合 形式實作

pyuic5不再支援–pyqt3-wrapper參數

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

pyrcc5 不再支援 -py2 或-py3參數

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 優化 合作性多重繼承 (Cooperative Multi-inheritance)的初始化方式

# PyQt5 調用父類的`__init__`方法.

def __init(self, **kwargs):

super().__init__(**kwargs)

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

PyQt5 退出時自動調用sip.setdestroyonexit()以禁用自動析構

Python解釋器退出PyQt4應用程式時會預設調用C++析構器處理所有它擁有的線程(這通常是以随機的順序,是以可能會導緻解析器崩潰),通過調用 sip.setdestroyonexit() 函數可以禁用。

PyQt5 總會自動調用 sip.setdestroyonexit() 函數.