PDA

View Full Version : Managining the behavior of items in view (like QGraphicsView)



Ronayn
27th June 2013, 17:56
I don't know if this is a general question about views, and I am probably not going to explain this well... but I am trying to figure out how to keep movable graphic items from going outside the view (or is it the scene? so confused.)

In the Simple Anchor Layout example, under QGraphicsView, a QGraphicsWidget is created. If you move it around, you'll notice that scroll bars appear and that you can move the widget past the edge of the view. I want to stop this scrolling behavior -- No matter where I move it, I always want the *WHOLE* widget to be visible.

Is that a property of the QGraphicsView, QGraphicsScene, or QGraphicsWidget? Or some combination?



(Is there is a "general" case for this? Because I've wondered how to keep subwindows within the frame of the QMdiArea exposed when the application comes up too.)

ChrisW67
27th June 2013, 23:07
What's visible in the view is driven by the QGraphicsView and is some subset of the QGraphicsScene (which is an abstract space that encompasses all of the QGraphicsItems in the scene). You should be able to use QGraphicsView::fitInView() with QGraphicsScene::sceneRect() and/or QGraphicsScene::sceneRectChanged() to scale everything to fit the scene into the view (within reason).

Ronayn
28th June 2013, 11:46
What's visible in the view is driven by the QGraphicsView and is some subset of the QGraphicsScene (which is an abstract space that encompasses all of the QGraphicsItems in the scene). You should be able to use QGraphicsView::fitInView() with QGraphicsScene::sceneRect() and/or QGraphicsScene::sceneRectChanged() to scale everything to fit the scene into the view (within reason).

Thanks Chris, but that answer doesn't solve the problem.

Let me try to clarify my question further...

I have a scene whose rectangle is (0,0,600,400). I've put a square in the middle of that scene and I've set the movable attribute for it. I begin to move the square to the left edge of the scene. When any part of my square touches the scene's border, any further movement of the square will be ignored (if the movement would cause any part of the square to fall outside the scene's rectangle). Basically, I want it so the scene's rectangle should acts like a true container (no graphical object can escape the container).

Hope I did I better job of explaining it.

wysota
28th June 2013, 12:46
Subclass the item and reimplement itemChange() to constrain movement of your item.

Ronayn
1st July 2013, 16:38
Subclass the item and reimplement itemChange() to constrain movement of your item.

Thank you wysota, I examined the help page on itemChange() and at first glance it appears to offer a solution. I am going to try it out later today; however, I am still learning the basics of how to program graphics. Still, I think its odd that I have to implement my own class to make graphic items that respect scene boundaries -- shouldn't this be something the QGraphicsView container does? (Just curious for an opinion here.)

Just for further into, I am trying to make a simple card game like War or two player Poker -- I want to make sure that the cards cannot be dragged out of the scene and that scene cannot be expanded. I've been trying to find example code, but so far I've not found anything.