[PyKDE] select an item in the QGraphicsItemGroup [and other thoughts]

Krystian Samp samp.krystian at gmail.com
Wed Nov 22 19:11:43 GMT 2006


Hi all,

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):

import sys
from PyQt4 import QtGui, QtCore

class MyItem(QtGui.QGraphicsItem):
    def __init__(self):
        QtGui.QGraphicsItem.__init__(self)

    def boundingRect(self):
        return QtCore.QRectF(0,0,10,10)

    def paint(self, painter, option, widget):
        pen = QtGui.QPen(QtCore.Qt.SolidLine)
        if self.isSelected():
            pen.setColor(QtGui.QColor(255, 0, 0))
        painter.setPen(pen)
        painter.drawEllipse(0,0,10,10)

class MyView(QtGui.QGraphicsView):
    def __init__(self):
        QtGui.QGraphicsView.__init__(self)

        self.scene = QtGui.QGraphicsScene(self)
        self.scene.setSceneRect(-100, -100, 200, 200)

        self.group = QtGui.QGraphicsItemGroup()
        i = MyItem()
        i.setPos(-10,0)
        self.group.addToGroup(i)
        self.group.addToGroup(MyItem())

        self.scene.addItem(self.group)
        self.setScene(self.scene)

app = QtGui.QApplication(sys.argv)
view = MyView()
view.show()
sys.exit(app.exec_())

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.

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?

thanks for your patience and help
best regards,
Krystian Samp
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20061122/1b4dacc7/attachment.html


More information about the PyQt mailing list