1

I want to make QGraphicsItems persistant using sqlalchemy. Easily combining the Base class with the PySide classes gives an error about metaclasses. The metaclsses subject are the magic of Python and I do not want to dive into that when this is not needed. Is there an easy way to solve this metaclass conflict?

class MarketItem(Base, QtGui.QGraphicsEllipseItem, QtGui.QListWidgetItem):
    """
    """
        __tablename__       = "marketitem"
    Id                  = Column(Integer(4), primary_key=True)
    name                = Column(String(40))
    x_pos               = Column(Integer(4))
    y_pos               = Column(Integer(4))
    def __init__(self, x_pos, y_pos, scene, name, style=QtCore.Qt.SolidLine,
                 rect=None, matrix=QtGui.QTransform(), cat = None):
        super(MarketItem, self).__init__()
        self.setFlags(QtGui.QGraphicsItem.ItemIsSelectable|
                      QtGui.QGraphicsItem.ItemIsMovable|
                      QtGui.QGraphicsItem.ItemIsFocusable)
        self.pos = QtCore.QPoint(x_pos, y_pos)
        self.x_pos = x_pos
        self.y_pos = y_pos
        self.name = name

Gives the error:

  File "C:\Users\Richard\Documents\manAmpl\aptana\mampl\pm15ConeModel.py", line 47, in <module>
    class MarketItem(Base, QtGui.QGraphicsEllipseItem, QtGui.QListWidgetItem):
TypeError: Error when calling the metaclass bases
    metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Thanks, Richard

Avaris
  • 35,883
  • 7
  • 81
  • 72
Richard
  • 135
  • 2
  • 10
  • 1
    It looks like a simple case of needing to [specify the metaclass as described here](http://code.activestate.com/recipes/204197-solving-the-metaclass-conflict/), but I am not confident enough to call this an answer. – khoxsey Jul 19 '12 at 18:54

1 Answers1

1

A similar question has been asked earlier and the answer to it helped me make a possible solution for you.

class CommonMetaclass(type(QtCore.Qt), type(Base)):
    pass

class MarketItem(QtGui.QGraphicsEllipseItem, QtGui.QListWidgetItem, Base):
    __metaclass__ = CommonMetaclass
    __tablename__ = "marketitem"
    ...

I was able to declare the class like this, not sure if it's actually usable.

If something isn't right, try changing order of the parent classes in both of these class definitions.

For Python 3 this snippet has to be slightly modified, but you said nothing about Python 3 and, sadly, version 2 is the 'default' right now...

Community
  • 1
  • 1
Oleh Prypin
  • 33,184
  • 10
  • 89
  • 99
  • Please explain more: what are the metaclasses of the base classes and the class MarketItem in this case? (the Qt class is an extension, a wrapper of the C language Qt library. The 'Base' class is a class produced by the sqlalchemy module via a call to declarative_base() method. The question does not show a __metaclass__ attribute declared for the derived class MarketItem, so it's metaclass is what?) – bootchk Nov 13 '12 at 16:01
  • Does error message want derived class's metaclass be a subset of the set of metaclass of all base classes, or just the ultimate base classes (for Qt classes, 'type' and for sqlalchemy classes what?)? – bootchk Nov 13 '12 at 16:07