我在嘗試添加帶有元類的mixin到基類爲QWidget的類時出現問題。我知道PyQt5 supports cooperative multiple inheritance,如果我的MixIn類沒有元類,那麼事情工作正常。但是,如果它有一個元類 - 無論是通過QWidgets共享pyqtWrapperType
元類或派生元類的話,我收到以下錯誤:PyQt5:派生的QWidget類中帶有元類的Mixin的問題
Process finished with exit code -1073741819 (0xC0000005)
爲腳本的其餘部分的代碼運行,但QWidget不顯示。這裏的基本代碼(剝離的方法,因爲我知道他們是沒有必要的問題)
import abc, sys
from PyQt5 import QtWidgets, QtCore
# Test Metaclass, will have more if metaclasses work
class MyMeta(abc.ABCMeta, QtCore.pyqtWrapperType):
def __init__(cls, name, bases, attrs):
super(MyMeta, cls).__init__(name, bases, attrs)
# MixIn class - ignore the calls to methods for now
# Have same issue if metaclass set to pyqtWrapperType
class LocatorWidget(metaclass=MyMeta):
def __init__(self, locator=None, name='', parameters={}, **kwargs):
super().__init__(**kwargs)
# self.setup_parameters(parameters)
self.locator = locator
self.name = name if name else ''
self.widgetType = self.__class__.__name__.replace('LW', '')
# self.setup()
# Derived class with a QWidget base
class LWComboBox(QtWidgets.QComboBox, LocatorWidget):
def __init__(self, locator, **kwargs):
super().__init__(locator=locator, **kwargs)
def main():
app = QtWidgets.QApplication(sys.argv)
# locator is class in full code, using this as filler for now
locator=[0,1,2,3]
cb = LWComboBox(locator=locator)
cb.addItems([str(x) for x in range(5)])
# Test to see if attribute is set
print(cb.locator)
window = QtWidgets.QDialog()
window.form = QtWidgets.QFormLayout()
window.form.addRow(cb)
window.setLayout(window.form)
window.show()
if __name__ == '__main__':
main()
我有元類衝突錯誤不:基類和派生類不具有不同的元類因爲MyMeta是派生自pyqtWrapperType。
如果這個過程不起作用,我想知道如果我應該堅持我之前的想法,那就是使用我想要在單獨的類中共享的屬性和方法來封裝小部件,小部件是其中一個屬性,但是直接用必要的抽象方法和屬性來繼承QWidgets會很好。
作爲一個供參考的我跑PyCharm 2016年2月3日在蟒蛇與PyQt5版本5.6(PyQt的不能在蟒蛇被更新到更高版本)
我沒有的問題'pyqtWrapperType'簡化版,存在;快速檢查表明我沒有最新版本的PyQt5。 (我有Anaconda和PyQt5 v 5.60)我會更新並應用您的修復程序並查看會發生什麼,但是在我使用的版本中將'pyqtWrapperType'更改爲'type(QtCore.QObject)'似乎沒有解決問題。 – Bill3000
在您的示例中修復了幾個不相關的錯誤之後,我發現使用我建議的更改對我來說工作得很好。 – ekhumoro
您使用的是什麼版本的PyQt?根據[http://python.6.x6.nabble.com/What-happend-to-PyQt5-QtCore-pyqtWrapperType-td5207614.html](這裏),pyqtWrapperType在PyQt 5.7中被刪除,並且仍然在5.6中可用。 Anaconda(我使用)[https://github.com/ContinuumIO/anaconda-issues/issues/1023](不支持PyQt 5.7)。這是一個版本問題,我不得不手動升級到5.7? 當我做'type(QObject)'時,它會在PyQt 5.6中爲我返回'pyqtWrapperType'。 – Bill3000