I’m creating a subclass of QGraphicsItem, and this subclass has different places that the user can click. My idea is for each component clicked to create a subclass of QGraphicsItem with mousePressEvent replaced. The problem is how can I merge this component into a subclass of QGraphicItem.
Here’s the code I’m trying, but I do not know how to show all the components in the paint method.
JavaScript
x
70
70
1
# -*- coding: utf-8 -*-
2
from PySide import QtGui, QtCore
3
4
class GraphicItemMain(QtGui.QGraphicsItem):
5
6
def __init__(self, x, y):
7
super(GraphicItemMain, self).__init__()
8
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, False)
9
self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, False)
10
self.setFlag(QtGui.QGraphicsItem.ItemIsFocusable, True)
11
self.setAcceptsHoverEvents(True)
12
self.x = x
13
self.y = y
14
15
def boundingRect(self):
16
return QtCore.QRectF(self.x, self.y, 100, 100)
17
18
def paint(self, painter, option, widget):
19
textComponent = GraphicItemTextClicked(5+self.x, 5+self.y)
20
ellipseComponent = GraphicItemEllipseClicked(5+self.x, 50+self.y)
21
# How do I print this components?
22
23
class GraphicItemTextClicked(QtGui.QGraphicsItem):
24
25
def __init__(self, x, y):
26
super(GraphicItemTextClicked, self).__init__()
27
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, False)
28
self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, False)
29
self.setFlag(QtGui.QGraphicsItem.ItemIsFocusable, True)
30
self.setAcceptsHoverEvents(True)
31
self.x = x
32
self.y = y
33
34
def mousePressEvent(self, event):
35
#Do something
36
QtGui.QGraphicsItem.mousePressEvent(self, event)
37
38
def boundingRect(self):
39
return QtCore.QRectF(self.x, self.y, 80, 30)
40
41
def paint(self, painter, option, widget):
42
painter.setPen(QtGui.QPen(QtGui.QColor(255, 0, 0), 1))
43
font = QtGui.QFont()
44
font.setPointSize(12)
45
painter.setFont(font)
46
painter.drawText(QtCore.QPointF(3+self.x, self.y), "Same Text")
47
48
class GraphicItemEllipseClicked(QtGui.QGraphicsItem):
49
50
def __init__(self, x, y):
51
super(GraphicItemEllipseClicked, self).__init__()
52
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, False)
53
self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, False)
54
self.setFlag(QtGui.QGraphicsItem.ItemIsFocusable, True)
55
self.setAcceptsHoverEvents(True)
56
self.x = x
57
self.y = y
58
59
def mousePressEvent(self, event):
60
#Do other thing
61
QtGui.QGraphicsItem.mousePressEvent(self, event)
62
63
def boundingRect(self):
64
return QtCore.QRectF(self.x, self.y, 25, 25)
65
66
def paint(self, painter, option, widget):
67
painter.setPen(QtGui.QPen(QtGui.QColor(0, 255, 0), 1))
68
painter.drawEllipse(self.x, self.y, 25, 25)
69
70
Advertisement
Answer
Thanks @SimonHibbs , the solution was to call textComponent.setParentItem(self) and ellipseComponent.setParentItem(self) on init method.
Solution:
JavaScript
1
70
70
1
from PySide import QtGui, QtCore
2
3
class GraphicItemMain(QtGui.QGraphicsItem):
4
5
def __init__(self, x, y):
6
super(GraphicItemMain, self).__init__()
7
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, False)
8
self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, False)
9
self.setFlag(QtGui.QGraphicsItem.ItemIsFocusable, True)
10
self.setAcceptsHoverEvents(True)
11
self.x = x
12
self.y = y
13
textComponent = GraphicItemTextClicked(5+self.x, 5+self.y)
14
textComponent.setParentItem(self)
15
ellipseComponent = GraphicItemEllipseClicked(5+self.x, 50+self.y)
16
ellipseComponent.setParentItem(self)
17
18
def boundingRect(self):
19
return QtCore.QRectF(self.x, self.y, 100, 100)
20
21
def paint(self, painter, option, widget):
22
# Paint samething
23
24
class GraphicItemTextClicked(QtGui.QGraphicsItem):
25
26
def __init__(self, x, y):
27
super(GraphicItemTextClicked, self).__init__()
28
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, False)
29
self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, False)
30
self.setFlag(QtGui.QGraphicsItem.ItemIsFocusable, True)
31
self.setAcceptsHoverEvents(True)
32
self.x = x
33
self.y = y
34
35
def mousePressEvent(self, event):
36
# Do something
37
QtGui.QGraphicsItem.mousePressEvent(self, event)
38
39
def boundingRect(self):
40
return QtCore.QRectF(self.x, self.y, 80, 30)
41
42
def paint(self, painter, option, widget):
43
painter.setPen(QtGui.QPen(QtGui.QColor(255, 0, 0), 1))
44
font = QtGui.QFont()
45
font.setPointSize(12)
46
painter.setFont(font)
47
painter.drawText(QtCore.QPointF(3+self.x, self.y), "Same Text")
48
49
class GraphicItemEllipseClicked(QtGui.QGraphicsItem):
50
51
def __init__(self, x, y):
52
super(GraphicItemEllipseClicked, self).__init__()
53
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, False)
54
self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, False)
55
self.setFlag(QtGui.QGraphicsItem.ItemIsFocusable, True)
56
self.setAcceptsHoverEvents(True)
57
self.x = x
58
self.y = y
59
60
def mousePressEvent(self, event):
61
# Do other thing
62
QtGui.QGraphicsItem.mousePressEvent(self, event)
63
64
def boundingRect(self):
65
return QtCore.QRectF(self.x, self.y, 25, 25)
66
67
def paint(self, painter, option, widget):
68
painter.setPen(QtGui.QPen(QtGui.QColor(0, 255, 0), 1))
69
painter.drawEllipse(self.x, self.y, 25, 25)
70