结合 Qgraphics 和 sqlalchemy 时出现元类错误

     2023-02-23     109

关键词:

【中文标题】结合 Qgraphics 和 sqlalchemy 时出现元类错误【英文标题】:Metaclass error when combining Qgraphics and sqlalchemy 【发布时间】:2012-07-17 05:54:24 【问题描述】:

我想使用 sqlalchemy 使 QGraphicsItems 持久化。轻松将 Base 类与 PySide 类结合起来会产生关于元类的错误。 metaclsses 主题是 Python 的魔力,我不想在不需要时深入研究它。 有没有简单的方法来解决这个元类冲突?

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

给出错误:

  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

谢谢,理查德

【问题讨论】:

这看起来像是一个需要specify the metaclass as described here 的简单案例,但我没有足够的信心将此称为答案。 【参考方案1】:

之前有人问过类似的问题,the answer 帮助我为您找到了可能的解决方案。

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

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

我能够像这样声明这个类,不确定它是否真的可用。

如果有问题,请尝试在这两个类定义中更改父类的顺序。

对于 Python 3,此 sn-p 必须稍作修改,但您对 Python 3 只字未提,遗憾的是,版本 2 现在是“默认”...

【讨论】:

请解释一下:在这种情况下,基类和 MarketItem 类的元类是什么? (Qt 类是扩展,是 C 语言 Qt 库的包装。“Base”类是 sqlalchemy 模块通过调用 declarative_base() 方法生成的类。问题没有显示 metaclass 为派生类MarketItem声明的属性,那么它的元类是什么?) 错误消息是否希望派生类的元类是所有基类的元类集的子集,或者只是最终的基类(对于 Qt 类,'type' 和 sqlalchemy 类是什么?)?

flask-sqlalchemy:数据未更新[重复]

】flask-sqlalchemy:数据未更新[重复]【英文标题】:flask-sqlalchemy:dataisnotupdated[duplicate]【发布时间】:2018-06-2515:27:53【问题描述】:我正在结合MySQL和FlaskAPI运行SQLAlchemy。我们对请求使用模型,并使用db.session.add()和db.session.commit()创... 查看详情

最初在 QGraphics 中移动滚动条

】最初在QGraphics中移动滚动条【英文标题】:InitiallymovedscrollbarinsideQGraphics【发布时间】:2019-09-0822:25:32【问题描述】:我正在尝试设置最初在QGraphicsScene小部件内移动的垂直和水平滚动条。以下代码应该移动条形并将它们设置... 查看详情

Qt - 使用 QTransform(或类似的),将内部 QRect 缩放到 QGraphics/从 QGraphics

】Qt-使用QTransform(或类似的),将内部QRect缩放到QGraphics/从QGraphics【英文标题】:Qt-UsingaQTransform(orsimilar),scaleinnerQRectto/fromQGraphics【发布时间】:2014-05-1615:19:36【问题描述】:一些背景-假设您有QGraphicsScene,并且只有一个视图,... 查看详情

QGraphics查看工件

】QGraphics查看工件【英文标题】:QGraphicsViewartefacts【发布时间】:2012-03-2512:04:45【问题描述】:我的QGraphicsView出现问题。我将QGraphicsView子类化以重新定义paintEvent。我想使用QGraphicsItem为节点绘制某种树,使用QGraphicsLineItem为边... 查看详情

结合继承yieldcontextmanagerrollback来解决问题。

 1、自己建立一个类去继承SQLALCHEMY 里面创建一个方法   调用SQLALchemy里面的函数,他会执行他的上下文  查看详情

SQLAlchemy:group_concat(case when ...)可能吗?

】SQLAlchemy:group_concat(casewhen...)可能吗?【英文标题】:SQLAlchemy:group_concat(casewhen...)possible?【发布时间】:2017-01-1214:21:09【问题描述】:我正在尝试将结合使用group_concat和casewhen的MySQL查询转换为SQLAlchemy:MySQL:group_concat(casewhenT... 查看详情

✎qt-doc—qgraphics图形视图框架&坐标系统(代码片段)

...染(OpenGLRendering)项分组(ItemGroups)组件和布局(WidgetsandLayouts)QGraphicsWidgetQGraphicsLayout嵌入QWidget组件(EmbeddedWidgetSupport)性能(Performance)浮点指令(FloatingPointInstructions)GraphicsViewFramework图形视图框架(GraphicsV 查看详情

✎qt-doc—qgraphics图形视图框架&坐标系统(代码片段)

...染(OpenGLRendering)项分组(ItemGroups)组件和布局(WidgetsandLayouts)QGraphicsWidgetQGraphicsLayout嵌入QWidget组件(EmbeddedWidgetSupport)性能(Performance)浮点指令(FloatingPointInstructions)GraphicsViewFramework图形视图框架(GraphicsV 查看详情

qt学习_qgraphics进阶学习笔记(代码片段)

QGraphics进阶学习1.保存图片函数QPixmapQWidget::grab(constQRect&rectangle=QRect(QPoint(0,0),QSize(-1,-1)))Rendersthewidgetintoapixmaprestrictedbythegivenrectangle.Ifthewidgethasanychildren,thentheyareals 查看详情

七夕专属博文-使用qgraphics画“红心“或“黑心“(含数学模型讲解)(代码片段)

说明实现的效果是这样的,使用QGraphicsEllipseItem和QGraphicsPolygonItem画“心”。比如来个红心再来个“黑心”:“心形”模型详解这里需要用到高中数学。①要了解圆的参数方程,其公式是这样的:x=a+r·cosθ和y... 查看详情

七夕专属博文-使用qgraphics画“红心“或“黑心“(含数学模型讲解)(代码片段)

说明实现的效果是这样的,使用QGraphicsEllipseItem和QGraphicsPolygonItem画“心”。比如来个红心再来个“黑心”:“心形”模型详解这里需要用到高中数学。①要了解圆的参数方程,其公式是这样的:x=a+r·cosθ和y... 查看详情

Qt - QGraphics (Shaped) 项目的选择

】Qt-QGraphics(Shaped)项目的选择【英文标题】:Qt-SelectionofQGraphics(Shaped)Item【发布时间】:2017-02-1323:56:39【问题描述】:[1/2]上下文你好!Qt为我们提供了创建高度定制的图形项目的方法。我们需要做的就是从QGraphicsItem继承并覆盖纯... 查看详情

从 QGraphicsScene 上的 QGraphicsLinearLayout 中删除 QGraphics 项

】从QGraphicsScene上的QGraphicsLinearLayout中删除QGraphics项【英文标题】:DeleteQGraphicsItemsfromQGraphicsLinearLayoutonQGraphicsScene【发布时间】:2012-12-0302:32:53【问题描述】:我遇到了一个非常令人沮丧的问题,试图在我的应用程序中删除qgraph... 查看详情

qgraphics图形视图框架的关系

#include<QtWidgets>intmain(intargc,char*argv[]){QApplicationa(argc,argv);QGraphicsScenescene(0,0,480,320);//场景所有的图形item都被必须附着在这个场景(scene)上面QGraphicsViewview(&scene);//视图场景上的所有图形item都被必须通过这个视 查看详情

qt中qgraphics类坐标映射关系详解(有图有真相,实例讲解)

如果你英文足够好,可以参考Qt帮助文档中的:TheGraphicsViewCoordinateSystem。---------------------------------------------------------------------------------------------------------------------------首先,先上显示界面图(Embededdialo 查看详情

Flask-SQLAlchemy 和 SQLAlchemy

】Flask-SQLAlchemy和SQLAlchemy【英文标题】:Flask-SQLAlchemyandSQLAlchemy【发布时间】:2015-03-2522:53:53【问题描述】:我正在建立一个小型网站,并且我已经在SQLAlchemy中拥有所有我的模型。该网站将发布一些离线计算的信息。只有结果会... 查看详情

sqlalchemy安装和使用

1、SQLAlchemy安装SQLAlchemy依赖mysql-python驱动,mysql-python目前只有支持py2的版本和mysql5.5的版本点我:mysql-python链接 版本:1.2.5点我:SQLAlchemy链接 版本:1.0.13安装SQLAlchemy,可以通过下载源码,也可以直接pip安装。安装mysql数... 查看详情

Sqlalchemy,原始查询和参数

】Sqlalchemy,原始查询和参数【英文标题】:Sqlalchemy,rawqueryandparameters【发布时间】:2015-05-2608:38:48【问题描述】:我正在尝试使用sqlalchemy执行原始sql查询,并想知道什么是“正确”的方法。我的查询如下(目前):db.my_session.exe... 查看详情