PDA

View Full Version : Incorrect selection rect of QGraphicsItemGroups containing offset QGraphicsPixmapItem



kerchen
9th June 2011, 18:49
I'm adding QGraphicPixmapItems to a QGraphicsItemGroup and I've encountered a problem: if I apply an offset to the pixmap items (via QGraphicPixmapItem::setOffset()), the selection rectangle for the group doesn't reflect that offset (i.e., the group's selection rectangle remains the bounding rectangle of all the pixmap items without any offset applied). Here's a (simplified) code snippet showing how in my class that's derived from QGraphicsItemGroup I add a pixmap item to the group:


void MyGraphicsItemGroup::addPixmapItem( const QPixmap& pixmap )
{
QGraphicsPixmapItem* gpi( new QGraphicsPixmapItem( pixmap ) );
QPointF offset = QPointF( -pixmap.width() / 2, -pixmap.height() / 2 );

this->addToGroup( gpi );
gpi->setOffset( offset );

/// ... do other stuff
}


I tried subclassing QGraphicsItemGroup and reimplementing boundingRect() so that it takes the offsets into account, but that didn't work (the selection rect was the same as before), leading me to believe that QGraphicsItemGroup must use some other function to figure out what the bounding rect of its items are for purposes of drawing the selection rectangle. It seems like childrenBoundingRect() might be a candidate, but it's not a virtual function, so I can't override its behavior. Does anyone know if what I'm trying to do is even possible? Any help or insights greatly appreciated!

BTW, I'm using Qt 4.5 + MSVC 2005/gcc 4.1.

Added after 34 minutes:

So, it turns out that calling setOffset() *before* adding the item to the group solves the problem. Yay for me!