[Qt-interest] GraphicsView::items() confusion

Martin Schröer gordon at gordonswelt.de
Tue Mar 10 19:27:36 CET 2009


Hi List.

I recently discovered something that made me wonder:
Consider a litte application using GraphicsView.

On the GraphicsView, there are a widget, embedded as QGraphicsWidget plus
three subclassed QGraphicsItem s ( subclassed in my custom class 
'imemGraphicsItem' ).

Ok, do have those items sized along with the viewport, i also subclassed 
GraphicsView, changing the resize event to:

void imemResizingGraphicsView::resizeEvent(QResizeEvent *event)
{
    ((QGraphicsWidget*) items().at(0))->setGeometry(0, 0, 
event->size().width(), event->size().height());
    ((imemGraphicsItem*) items().at(0))->updateGeometry(0, 0, 
event->size().width(), event->size().height());
    setSceneRect(0, 0, event->size().width(), event->size().height());
}

So, my former two objects on the graphics view would scale along with it.

Well, while i was trying to cycle through items() and doing some dirty 
type casting so the
right method would be called ( setGeometry() for QGraphicsWidgets and 
updateGeometry for imemGraphicsItems )
i noted that a simple repetition of the call with .at(0) would update 
all included items ?

see here:

void imemResizingGraphicsView::resizeEvent(QResizeEvent *event)
{
    ((QGraphicsWidget*) items().at(0))->setGeometry(0, 0, 
event->size().width(), event->size().height());
  
    for (int i = 0; i < items().size() -1; ++i) {
        ((imemGraphicsItem*) items().at(0))->updateGeometry(0, 0, 
event->size().width(), event->size().height());
    }

    setSceneRect(0, 0, event->size().width(), event->size().height());
}

[ remember: there is one QGraphicsWidget on the GraphicsView plus three 
imemGraphicsItems. ]

Isn't that a bit odd ?


Martin


P.S.: I'm still looking for some better solution to do those method 
calls for those two types of items,
as i'm stuck at doing a type cast for the items() list, somewhat like
QList<QGraphicsWidget *> wids = ((QList<QGraphicsWidget *>) items())
and somewhat like that for my own subclass, imemGraphicItem.
Any help on that would be highly appreciated.



More information about the Qt-interest-old mailing list