Reputation: 41
I'm having issues trying to add a mixin with a metaclass to a class whose base is a QWidget. I'm aware that PyQt5 supports cooperative multiple inheritance and if my MixIn class has no metaclass then things work fine. However, if it has a metaclass - whether it be the pyqtWrapperType
metaclass shared by QWidgets or a derived metaclass, then I receive the following error:
Process finished with exit code -1073741819 (0xC0000005)
The code for the rest of the script runs, but the QWidget does not show up. Here's the base code (stripped of the methods since I know they're not necessary for the problem)
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()
I am not having the metaclass conflict error: The base class and derived classes do not have different metaclasses because MyMeta is derived from pyqtWrapperType.
If this process doesn't work I'm wondering if I should stick with my previous idea which was enveloping the widget with the attributes and methods I want to share in a separate class, the widget being one of the attributes, but it'll be nice to subclass QWidgets directly with the necessary abstract methods and attributes.
As an FYI I an running PyCharm 2016.2.3 in Anaconda with PyQt5 version 5.6 (PyQt can't be updated to later versions in Anaconda)
Upvotes: 4
Views: 1833
Reputation: 120698
The pyqtWrapperType
no longer exists. If you want the equivalent type in PyQt5, you can use either:
pyqtWrapperType = type(QtCore.QObject)
or:
from sip import wrappertype as pyqtWrapperType
Upvotes: 3