0

I have several QGraphicItems in a QGraphicsScene. I want to modify only some of them when the QGraphicsView containing the scene gets resized. The reason is that I have painted a grid respective to the view.

So how can I identify the items making up the grid in a QGraphivcsView::resizeEvent()?

Would it be possible to adjust the scene in a way that a given area (sceneRect) is always filling the complete view?

HWende
  • 1,705
  • 4
  • 18
  • 30
  • For your last question you might be able to do it with `QGraphicsView::scale` if you know the new size and the old size which seems possible with: `QResizeEvent::oldSize()` For the rest I don't really understand, can't you use the *QGraphicsItem ? – Leo Jun 22 '12 at 12:41

1 Answers1

1

So how can I identify the items making up the grid in a QGraphivcsView::resizeEvent()?

One way would be to simply use dynamic_cast and QGrahpicsScene::items():

foreach( QGraphicsItem *item, myScene->items() )
{
  GridItem *gridItem = dynamic_cast<GridItem*>( item );
  if( gridItem )
  {
    // Apply appropriate transformation here
  }
}

A slightly more "Qt" way to do the above would be to ensure your QGraphicsItem subclass reimplements QGraphicsItem::type()

foreach( QGraphicsItem *item, myScene->items() )
{
  if( item->type() == GridItem::Type )
  {
    // Apply appropriate transformation here
  }
}

Would it be possible to adjust the scene in a way that a given area (sceneRect) is always filling the complete view?

QGraphicsView::fitInView() should do the trick

Also, while I'm not entirely sure what you're trying to accomplish, it sounds to me that you may want to check out the QGraphicsItem::ItemIgnoresTransformations flag.

myItem->setFlag( QGraphicsItem::ItemIgnoresTransformations )

which makes it so that any item with the given flag will not be affected by changes in the zoom level.

Chris
  • 17,119
  • 5
  • 57
  • 60