简体   繁体   English

如何在一个类中继承 QGraphicsRectItem 和 QGraphicsEllipseItem?

[英]How can I subclass QGraphicsRectItem and QGraphicsEllipseItem in one class?

I'm using Pyside2 and have a UI that uses QGraphicsView and a QGraphicsScene.我正在使用 Pyside2 并且有一个使用 QGraphicsView 和 QGraphicsScene 的 UI。

Right now I have two separate classes that subclass QGraphicsEllipseItem and QGraphicsRectItem like this:现在我有两个独立的类,它们像这样子类化 QGraphicsEllipseItem 和 QGraphicsRectItem:

class MyRectButton(QGraphicsRectItem):

    def contextMenuEvent(self, event):
        # custom context menu
        pass

    def custom_method_A(self):
        # add a custom method
        pass

    def custom_method_B(self):
        # add a custom method
        pass

class MyEllipseButton(QGraphicsEllipseItem):

    def contextMenuEvent(self, event):
        # custom context menu
        pass

    def custom_method_A(self):
        # add a custom method
        pass

    def custom_method_B(self):
        # add a custom method
        pass

Rather than have the redundant methods in both classes I'd like one class that can either be a rectangle or an ellipse like:而不是在两个类中都有冗余方法,我想要一个可以是矩形或椭圆形的类,例如:

class MyButton():
    def __init__(self,shape='Ellipse'):
        pass

    def contextMenuEvent(self, event):
        # custom context menu
        pass

    def custom_method_A(self):
        # add a custom method
        pass

    def custom_method_B(self):
        # add a custom method
        pass


You can create a class that implements the common functionality and then the buttons inherit from the item and the common class:您可以创建一个实现通用功能的类,然后按钮从项目和通用类继承:

from PyQt5 import QtCore, QtWidgets


class Foo:
    def contextMenuEvent(self, event):
        print("contextMenuEvent")

    def custom_method_A(self):
        print("custom_method_A")

    def custom_method_B(self):
        print("custom_method_B")


class MyRectButton(QtWidgets.QGraphicsRectItem, Foo):
    pass


class MyEllipseButton(QtWidgets.QGraphicsEllipseItem, Foo):
    pass


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    scene = QtWidgets.QGraphicsScene()
    view = QtWidgets.QGraphicsView(scene)

    it_rect = MyRectButton(QtCore.QRectF(-50, -30, 100, 100))
    it_ellipse = MyEllipseButton(QtCore.QRectF(50, 50, 100, 100))

    scene.addItem(it_rect)
    scene.addItem(it_ellipse)

    for it in (it_rect, it_ellipse):
        it.custom_method_A()
        it.custom_method_B()

    view.resize(640, 480)
    view.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使子类覆盖它,我如何确保始终调用我的类的方法之一? - How can I ensure that one of my class's methods is always called even if a subclass overrides it? 如何通过单击 QGraphicsView/QGraphicsScene 画布绘制可(正确)移动的 QGraphicsRectItem? - How can I draw a QGraphicsRectItem that is (properly) movable by clicking on a QGraphicsView/QGraphicsScene canvas? 如何(在运行时)检查一个类是否是另一个类的子类? - How do I check (at runtime) if one class is a subclass of another? Python:如何对要嵌套的类进行子类化? - Python: How can I subclass a class I'm nesting in? 如何将父 class 的实例转换为 python 中的子类之一? - How could I convert an instance of a parent class to one of its subclass in python? 如何在子类中调用父类? - How do I call on a parent class in a subclass? 选择后如何更改QGraphicsEllipseItem的颜色? - how to change color of QGraphicsEllipseItem when it selected? 抽象类可以是子类吗? - Can an abstract class be a subclass? 如何获得子类以返回其自身的副本,而不是其继承自的父类? - How can I get a subclass to return a copy of itself, rather than the parent class it is inheriting from? 如何确定Django模型中的类实例是否是另一个模型的子类? - How can I determine if instance of class from Django model is subclass of another model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM