[Qt-interest] QGraphicsScene: get bounding box of selected items?
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Mon Dec 27 11:28:46 CET 2010
On 2010-12-27 Roland Roland Mueller wrote:
> This exists:
> http://doc.qt.nokia.com/latest/qgraphicsscene.html#itemsBoundingRect
>
> which gives me the bounding rect of /all/ items - is there an
> analogon/convenience method for getting the bounding rect of the
> /selected/ items only?
> ...
> I'll neer tried that but according to the documentation it should be
> possible to get the selectionArea() of the scene which returns an
> QPainterPath object. The bounding rect should be then the boundingRect() of the path:
Thanks for the quick response! I don't think selectionArea is what I want: I think the "selection area" refers to that "rubber band" thing with which you can define an area of selection. All items which lie within or intersect with that area are then selected. Also, depending on the selection mode, items can really lie partly outside of this selection area, so the bounding box of the selection area is potentially smaller (and larger off course as well) than the bounding box of the selected items.
And in my case I don't even have such a selection area, I simply click on the items to toggle the selection.
In the meantime I have the following solution:
QRect sourceRect;
QList<QGraphicsItem *> selectedItems = m_graphicsScene.selectedItems();
foreach(QGraphicsItem *current, selectedItems) {
sourceRect = sourceRect.united(current->sceneBoundingRect());
}
...
m_graphicsScene.render(&painter, QRectF(), sourceRect);
which then renders the content of the "source rectangle" with a QPainter into the desired QImage. Easy enough. I just though I could save a couple of lines of code by using some QGraphicsScene convenience method ;)
Note my mistake in my previous post, one should off course use QGraphicsItem #sceneBoundingRect instead of just boundingRect().
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
More information about the Qt-interest-old
mailing list