Hi all,<br><br>I want to gather some of my QGraphicsItems in a parent node to be able to translate and rotate the whole set easily. Additionally I want to be able to select on of the items inside the set. So here is one of the approaches I've tried - it uses QGraphicsItemGroup (I made it as short as possible):
<br><br>import sys<br>from PyQt4 import QtGui, QtCore<br><br>class MyItem(QtGui.QGraphicsItem):<br> def __init__(self):<br> QtGui.QGraphicsItem.__init__(self)<br> <br> def boundingRect(self):<br> return
QtCore.QRectF(0,0,10,10)<br> <br> def paint(self, painter, option, widget):<br> pen = QtGui.QPen(QtCore.Qt.SolidLine)<br> if self.isSelected():<br> pen.setColor(QtGui.QColor(255, 0, 0))<br>
painter.setPen(pen)<br> painter.drawEllipse(0,0,10,10)<br><br>class MyView(QtGui.QGraphicsView):<br> def __init__(self):<br> QtGui.QGraphicsView.__init__(self)<br><br> self.scene = QtGui.QGraphicsScene
(self)<br> self.scene.setSceneRect(-100, -100, 200, 200)<br><br> self.group = QtGui.QGraphicsItemGroup()<br> i = MyItem()<br> i.setPos(-10,0)<br> self.group.addToGroup(i)<br> self.group.addToGroup
(MyItem())<br> <br> self.scene.addItem(self.group)<br> self.setScene(self.scene)<br><br>app = QtGui.QApplication(sys.argv)<br>view = MyView()<br>view.show()<br>sys.exit(app.exec_())<br><br>The problem is that QGraphicsItemGroup does not allow me to select items which are inside the set. I've tried to set the
QGraphicsItem.ItemIsSelectable flag for MyItem and QGraphicsItemGroup but it does not help. Interesting thing is that when you change QGraphicsItemGroup to QGraphicsEllipseItem (and probably to other Item types as well) and use setParentItem() to attach the items to it you will be able to select the items. What is the logic behind it? I've also tried to use setParentItem() in combination with QGraphicsItemGroup(). Am I missing something? I don't want to subclass QGraphicsItem to build my own group item or to use Ellipse to gather all the items :] For me it looks like the inconsistency between QGraphicsItemGroup and other Items which inherit from QGraphicsItem - but I hope I'm wrong.
<br><br>Another observation is that if you remove QGraphicsItemGroup from the scene the items within the group are not deleted (I mean underlying C/C++ objects). This is completely different behavior than this discussed recently in the mailing list. Using
e.g. QGraphicsEllipseItem along with setParentItem() method you can easily destroy all underlying C/C++ objects by removing the parent node. QGraphicsItemGroup behaves differently, or maybe I'm missing something once again?
<br><br>thanks for your patience and help<br>best regards,<br>Krystian Samp<br>