派生QWidget类中具有元类的Mixin问题

派生QWidget类中具有元类的Mixin问题

本文介绍了PyQt5:派生QWidget类中具有元类的Mixin问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试向基于QWidget的类中添加带有元类的mixin时遇到问题.我知道PyQt5 支持协同多重继承,并且如果我的MixIn类具有没有元类,然后一切正常.但是,如果它有一个元类-无论是QWidgets共享的pyqtWrapperType元类还是派生的元类,那么我会收到以下错误:

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)

脚本其余部分的代码运行,但是QWidget不显示.这是基本代码(剥离了方法,因为我知道它们对于解决问题不是必需的)

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()

没有具有元类冲突错误:基类和派生类没有不同的元类,因为MyMeta是从pyqtWrapperType派生的.

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.

如果该过程不起作用,我想知道我是否应该坚持我以前的想法,即用要在单独的类中共享的属性和方法来封装小部件,小部件是属性之一,但是最好直接使用必要的抽象方法和属性来将QWidgets子类化.

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.

作为FYI,我在Anaconda中使用PyQt5版本5.6运行PyCharm 2016.2.3(在Anaconda中无法将PyQt更新为更高版本)

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)

推荐答案

pyqtWrapperType不再存在.如果要在PyQt5中使用等效类型,可以使用以下任一方法:

The pyqtWrapperType no longer exists. If you want the equivalent type in PyQt5, you can use either:

pyqtWrapperType = type(QtCore.QObject)

或:

from sip import wrappertype as pyqtWrapperType

这篇关于PyQt5:派生QWidget类中具有元类的Mixin问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:32