Results 1 to 5 of 5

Thread: Managining the behavior of items in view (like QGraphicsView)

  1. #1
    Join Date
    May 2010
    Posts
    24
    Thanks
    14
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Managining the behavior of items in view (like QGraphicsView)

    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.)

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,641
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Managining the behavior of items in view (like QGraphicsView)

    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).

  3. #3
    Join Date
    May 2010
    Posts
    24
    Thanks
    14
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Managining the behavior of items in view (like QGraphicsView)

    Quote Originally Posted by ChrisW67 View Post
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Managining the behavior of items in view (like QGraphicsView)

    Subclass the item and reimplement itemChange() to constrain movement of your item.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    Ronayn (30th June 2013)

  6. #5
    Join Date
    May 2010
    Posts
    24
    Thanks
    14
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Managining the behavior of items in view (like QGraphicsView)

    Quote Originally Posted by wysota View Post
    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.

Similar Threads

  1. erratic behavior of graphics items
    By sajis997 in forum Qt Programming
    Replies: 20
    Last Post: 9th February 2012, 10:24
  2. Replies: 4
    Last Post: 6th December 2011, 09:27
  3. Question about behavior of QGraphicsView/QGraphicsScene
    By kennethadammiller in forum Newbie
    Replies: 0
    Last Post: 22nd July 2010, 09:56
  4. Strange behavior of checkable menu items..
    By jiapei100 in forum Qt Tools
    Replies: 1
    Last Post: 28th October 2009, 19:58
  5. QGraphicsView Behavior
    By chaoticbob in forum Qt Programming
    Replies: 1
    Last Post: 15th October 2009, 10:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.