0
QGraphicsItem의 하위 클래스를 만들고 있는데이 하위 클래스는 사용자가 클릭 할 수있는 다른 장소가 있습니다. 제 생각은 mousePressEvent를 대체하여 QGraphicsItem의 하위 클래스를 만들기 위해 클릭 한 각 구성 요소에 대한 아이디어입니다. 문제는이 구성 요소를 QGraphicItem의 하위 클래스로 병합 할 수있는 방법입니다.QGraphicsItem을 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)
당신은 더 나은 나에게 설명 할 수 있습니까? – eyllanesc
자식을 표시하려면 상위 QGraphicsItem 페인트 메서드에서 아무 것도 수행 할 필요가 없습니다. 이는 자체 페인트 방법으로 처리됩니다. 자식 메소드를 페인트 메소드가 아닌 부모의 __init__ 메소드로 추가하십시오. 그렇지 않으면 paint()가 호출 될 때마다 새로운 자식이 다시 추가됩니다. –
@SimonHibbs에게 감사드립니다. 해결책은 __init__ 메소드에서 textComponent.setParentItem (self) 및 ellipseComponent.setParentItem (self)를 호출하는 것이 었습니다. –