Results 1 to 5 of 5

Thread: Graphics View Framework - Coordinates and stuff

  1. #1
    Join Date
    Aug 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Graphics View Framework - Coordinates and stuff

    Greetings fellow Trolls,

    I'm having some trouble understanding the QGraphics View Framework.
    Or, to be more precise, how to use the Framework for a simple 2D Game I want to write.

    In this yet unwritten game I would like to display a Sprite inside several rooms. The rooms all have the same size. Within a room there is always the player controlled sprite and some stuff, doors, pictures on the wall, some furniture.

    In the KAsteroid example the Widgets size is set to 640,480 and the Screen coordinates to 0,0,640,440

    That would be enough for me. I can position my Items in the Scene and I know where they will appear on the View. But this way seems to be rather limited. So I wonder is it possible to set some Scene coordinates and allow the Widget to be resized in a way that the Items will still fill the whole of it?

    Taking the Asteroids example again, what would happen if I resized the widget, to 800x600 ?
    Where would the Items go? I'd assume you'd have a 800x600 Widget with only 640x480 showing the game.

    Can the Graphics View Framework be told to fill the Widget with the whole scene? So I can paint my Items in Scene coordinates and be sure that they will scale to fit the avaible space?

    And how could that be done if I want to add networking support?
    Can one player run the game in Fullscreen at 1280x1024 and some other in a 640x480 window and still allow mouse positions grabbed in one to correctly translate to the other?

    I would be really nice if I could just go and tell my scene to be 0,0,100,100 or whatever, draw my items in that system and have it displayed on any widget size, be it full screen or windowed mode, local or remote.

    Is there a way to do that? Or is it best to copy the ported Asteroids example?

    As far as I can tell my screen->setSceneRect gets completly ignored.
    I did a simply screen->setSceneRect(0,0,640,440)

    I also captured Mouse Events like this
    Qt Code:
    1. QString tmp;
    2. QPointF position = mouseEvent->scenePos();
    3. tmp += QString("Mouse here: %1, %2").arg(position.x()).arg(position.y());
    4. QGraphicsTextItem* test = this->addText(tmp);
    5.  
    6. test->setPos(position);
    To copy to clipboard, switch view to plain text mode 


    If my Widget (and therefore my View) is at 640x480 everything works as expected.
    That is my mouse clicks appear where I clicked with coords from 0,0 to 640,440

    If I do a resize, I can have mouse clicks way, way outside at -100,-150 or 700,900.

    So, I hope I'm getting my question across. Kinda having trouble with them english words.


    I'm really impressed with the forum so far and I hope you guys can help me.

    Regards from Germany,
    SkyRat

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

    Default Re: Graphics View Framework - Coordinates and stuff

    Quote Originally Posted by SkyRat View Post
    So I wonder is it possible to set some Scene coordinates and allow the Widget to be resized in a way that the Items will still fill the whole of it?
    The scene has a defined size and the view can show any portion of the scene with an arbitrary scale. So in your situation if your scene is smaller than your view, you'll have to scale the view so that the scene occupies the whole viewport. Fortunately for you there is a QGraphicsView::fitInView method which will do it for you. Unfortunately you have to call it every time the view is resized (so it's best to do it from its resizeEvent).

    Taking the Asteroids example again, what would happen if I resized the widget, to 800x600 ?
    Where would the Items go? I'd assume you'd have a 800x600 Widget with only 640x480 showing the game.
    The scene would be centered in the view.


    I would be really nice if I could just go and tell my scene to be 0,0,100,100 or whatever, draw my items in that system and have it displayed on any widget size, be it full screen or windowed mode, local or remote.

    Is there a way to do that?
    If you didn't use the graphicsView but a plain widget I'd suggest using setWindow() and setViewport() - if you want the scene to always fit the view, the advantages of using the graphics view are limited (although it can still be quite helpful).


    I also captured Mouse Events like this
    Qt Code:
    1. QString tmp;
    2. QPointF position = mouseEvent->scenePos();
    3. tmp += QString("Mouse here: %1, %2").arg(position.x()).arg(position.y());
    4. QGraphicsTextItem* test = this->addText(tmp);
    5.  
    6. test->setPos(position);
    To copy to clipboard, switch view to plain text mode 


    If my Widget (and therefore my View) is at 640x480 everything works as expected.
    That is my mouse clicks appear where I clicked with coords from 0,0 to 640,440

    If I do a resize, I can have mouse clicks way, way outside at -100,-150 or 700,900.
    In mouse events you should always use scene coordinates, preferably catching those events in the scene or in items themselves and not in the view.

  3. #3
    Join Date
    Aug 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Graphics View Framework - Coordinates and stuff

    Fortunately for you there is a QGraphicsView::fitInView method which will do it for you. Unfortunately you have to call it every time the view is resized (so it's best to do it from its resizeEvent).
    Sounds great, thanks a lot

    The scene would be centered in the view.
    Yeah, I assumed as much.

    If you didn't use the graphicsView but a plain widget I'd suggest using setWindow() and setViewport() - if you want the scene to always fit the view, the advantages of using the graphics view are limited (although it can still be quite helpful).
    This I dont understand. I thought my scene coordinates would stay the same, if I used the fitInView methode. From the documentation it looks like it would only scale the view and leave the scene untouched.

    In mouse events you should always use scene coordinates, preferably catching those events in the scene or in items themselves and not in the view.
    Yeah, I did that. The code I posted is from my Screen class which is based on the QGraphicsScene. I implemented the mousePressEvent ( QGraphicsSceneMouseEvent * mouseEvent )

    It looks to me as I can get any position I want from the mouse event. A mouseEvent->pos() gives me View coordinates. I need to use the ->scenePos() methode. At least I think I need to.

    Anyway, thanks for your help.
    I'll play around with the fitInView methode. I still might force the user to use a predefined Widget size, it would make things a lot easier apparently.
    Last edited by SkyRat; 5th August 2008 at 20:34.

  4. #4
    Join Date
    Aug 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Graphics View Framework - Coordinates and stuff

    Just did some quick 'n dirty tests.
    At the moment I do resizing by menu actions.

    In my slot for those I do something like this:
    Qt Code:
    1. void Game::res_to_800(void)
    2. {
    3. //QMessageBox::critical(this,"FOO","BAR");
    4. this->resize(800,600);
    5. myView->fitInView(screen->sceneRect(),Qt::KeepAspectRatio);
    6. return;
    7. }
    To copy to clipboard, switch view to plain text mode 

    This works as expected, really nice and simple
    Hope it will work within as well inside a resizeEvent.

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

    Default Re: Graphics View Framework - Coordinates and stuff

    Quote Originally Posted by SkyRat View Post
    This I dont understand. I thought my scene coordinates would stay the same, if I used the fitInView methode. From the documentation it looks like it would only scale the view and leave the scene untouched.
    Scene remains untouched. You zoom the view so that its viewport is filled with the scene.

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.