我怎样才能将class QGraphicsRectItem 和QGraphicsEllipseItem 合并为一个class?
How can I subclass QGraphicsRectItem and QGraphicsEllipseItem in one class?
我正在使用 Pyside2,并且有一个使用 QGraphicsView 和 QGraphicsScene 的 UI。
现在我有两个单独的 class 子 class 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
与其在两个 classes 中使用冗余方法,我想要一个 class 可以是矩形或椭圆形的方法,例如:
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
您可以创建一个实现通用功能的 class,然后按钮继承自项目和通用功能 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_())
我正在使用 Pyside2,并且有一个使用 QGraphicsView 和 QGraphicsScene 的 UI。
现在我有两个单独的 class 子 class 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
与其在两个 classes 中使用冗余方法,我想要一个 class 可以是矩形或椭圆形的方法,例如:
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
您可以创建一个实现通用功能的 class,然后按钮继承自项目和通用功能 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_())