我正在创建QGraphicsItem的子类,并且此子类具有用户可以单击的不同位置。 我的想法是点击每个组件创建一个QGraphicsItem的子类,并替换mousePressEvent。问题是如何将此组件合并到QGraphicItem的子类中。
这是我正在尝试的代码,但我不知道如何在paint方法中显示所有组件。
# -*- coding: utf-8 -*-
from PySide import QtGui, QtCore
class GraphicItemMain(QtGui.QGraphicsItem):
def __init__(self, x, y):
super(GraphicItemMain, self).__init__()
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, False)
self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, False)
self.setFlag(QtGui.QGraphicsItem.ItemIsFocusable, True)
self.setAcceptsHoverEvents(True)
self.x = x
self.y = y
def boundingRect(self):
return QtCore.QRectF(self.x, self.y, 100, 100)
def paint(self, painter, option, widget):
textComponent = GraphicItemTextClicked(5+self.x, 5+self.y)
ellipseComponent = GraphicItemEllipseClicked(5+self.x, 50+self.y)
#How I print this components?
class GraphicItemTextClicked(QtGui.QGraphicsItem):
def __init__(self, x, y):
super(GraphicItemTextClicked, self).__init__()
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, False)
self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, False)
self.setFlag(QtGui.QGraphicsItem.ItemIsFocusable, True)
self.setAcceptsHoverEvents(True)
self.x = x
self.y = y
def mousePressEvent(self, event):
#Do something
QtGui.QGraphicsItem.mousePressEvent(self, event)
def boundingRect(self):
return QtCore.QRectF(self.x, self.y, 80, 30)
def paint(self, painter, option, widget):
painter.setPen(QtGui.QPen(QtGui.QColor(255, 0, 0), 1))
font = QtGui.QFont()
font.setPointSize(12)
painter.setFont(font)
painter.drawText(QtCore.QPointF(3+self.x, self.y), "Same Text")
class GraphicItemEllipseClicked(QtGui.QGraphicsItem):
def __init__(self, x, y):
super(GraphicItemEllipseClicked, self).__init__()
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, False)
self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, False)
self.setFlag(QtGui.QGraphicsItem.ItemIsFocusable, True)
self.setAcceptsHoverEvents(True)
self.x = x
self.y = y
def mousePressEvent(self, event):
#Do other thing
QtGui.QGraphicsItem.mousePressEvent(self, event)
def boundingRect(self):
return QtCore.QRectF(self.x, self.y, 25, 25)
def paint(self, painter, option, widget):
painter.setPen(QtGui.QPen(QtGui.QColor(0, 255, 0), 1))
painter.drawEllipse(self.x, self.y, 25, 25)
答案 0 :(得分:1)
感谢@SimonHibbs,解决方案是在 init 方法上调用textComponent.setParentItem(self)和ellipseComponent.setParentItem(self)。
解决方案:
from PySide import QtGui, QtCore
class GraphicItemMain(QtGui.QGraphicsItem):
def __init__(self, x, y):
super(GraphicItemMain, self).__init__()
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, False)
self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, False)
self.setFlag(QtGui.QGraphicsItem.ItemIsFocusable, True)
self.setAcceptsHoverEvents(True)
self.x = x
self.y = y
textComponent = GraphicItemTextClicked(5+self.x, 5+self.y)
textComponent.setParentItem(self)
ellipseComponent = GraphicItemEllipseClicked(5+self.x, 50+self.y)
ellipseComponent.setParentItem(self)
def boundingRect(self):
return QtCore.QRectF(self.x, self.y, 100, 100)
def paint(self, painter, option, widget):
#Paint samething
class GraphicItemTextClicked(QtGui.QGraphicsItem):
def __init__(self, x, y):
super(GraphicItemTextClicked, self).__init__()
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, False)
self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, False)
self.setFlag(QtGui.QGraphicsItem.ItemIsFocusable, True)
self.setAcceptsHoverEvents(True)
self.x = x
self.y = y
def mousePressEvent(self, event):
#Do something
QtGui.QGraphicsItem.mousePressEvent(self, event)
def boundingRect(self):
return QtCore.QRectF(self.x, self.y, 80, 30)
def paint(self, painter, option, widget):
painter.setPen(QtGui.QPen(QtGui.QColor(255, 0, 0), 1))
font = QtGui.QFont()
font.setPointSize(12)
painter.setFont(font)
painter.drawText(QtCore.QPointF(3+self.x, self.y), "Same Text")
class GraphicItemEllipseClicked(QtGui.QGraphicsItem):
def __init__(self, x, y):
super(GraphicItemEllipseClicked, self).__init__()
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, False)
self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, False)
self.setFlag(QtGui.QGraphicsItem.ItemIsFocusable, True)
self.setAcceptsHoverEvents(True)
self.x = x
self.y = y
def mousePressEvent(self, event):
#Do other thing
QtGui.QGraphicsItem.mousePressEvent(self, event)
def boundingRect(self):
return QtCore.QRectF(self.x, self.y, 25, 25)
def paint(self, painter, option, widget):
painter.setPen(QtGui.QPen(QtGui.QColor(0, 255, 0), 1))
painter.drawEllipse(self.x, self.y, 25, 25)