问题
I am trying to create an AbstractClass using both abc.ABCMeta and QObject as parents and cannot seems to make it work.
Here is the Base class init. I have Pyqt5 and python 2.7
pyqtWrapperType = type(QObject)
class ParamsHandler(abc.ABCMeta, pyqtWrapperType):
def __init__(self, device_model, read_only=False):
super(ParamsHandler, self).__init__()
self.cmd_to_get_data = None
self.device_model = device_model
class ConfigParamsHandler(ParamsHandler):
def __init__(self, device_model):
super(ConfigParamsHandler, self).__init__(device_model)
self.cmd_to_get_data = Commands.CONFIG_PARAMS
I get a TypeError('new() takes exactly 4 arguments (2 given)',) I also have Pycharm suggesting that I use cls instead of self
If I supply 4 arguments, it asks for a string as the first argument.
回答1:
I solved it using this approach instead:
class ParamsHandler_Meta(type(QObject), type(abc.ABCMeta)):
pass
class ParamsHandlerClass(QObject):
pass
class ParamsHandler(ParamsHandlerClass):
__metaclass__ = ParamsHandler_Meta
def __init__(self, device_model, cmd_to_get_data, read_only=False):
super(ParamsHandler, self).__init__()
self.cmd_to_get_data = cmd_to_get_data
回答2:
abc.ABCMeta
is supposed to be used as a metaclass. So instead of extending it, try:
class ParamsHandler(pyqtWrapperType):
__metaclass__ = abc.ABCMeta
来源:https://stackoverflow.com/questions/49384161/python-mixing-pyqt5-and-abc-abcmeta