Results 1 to 4 of 4

Thread: co-ordinate confusion: scenePos()

  1. #1
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Question co-ordinate confusion: scenePos()

    Hello!

    I'm learning Qt's graphics framework by way of playing with a simple application that draws some rectangles on a scene. I understand that item co-ordinates are given in terms of the item's local co-ordinates, such that for a given item, the position is (0, 0). What I don't understand is why calling scenePos() on all the items returns (0, 0) for each one. Qt isn't lying, but how can the scenePos() for items that appear in the expected different locations be the same, and specifically why are they all 0? Here is the code:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. scene = new QGraphicsScene();
    5. scene->setSceneRect(0, 0, 1024, 768);
    6. QGraphicsRectItem *rect1 = scene->addRect(scene->width() / 4, scene->height() / 4, 10, 10);
    7. QGraphicsRectItem *rect2 = scene->addRect(scene->width() / 3, scene->height() / 3, 20, 20);
    8. QGraphicsRectItem *rect3 = scene->addRect(scene->width() / 2, scene->height() / 2, 30, 30);
    9.  
    10. qDebug() << rect1->pos() << ", " << rect2->pos() << ", " << rect3->pos(); // 3x QPointF(0, 0) -- fine
    11. qDebug() << rect1->scenePos() << ", " << rect2->scenePos() << ", " << rect3->scenePos(); // 3x QPointF(0, 0) -- what?
    12.  
    13. view = new QGraphicsView(scene, this);
    14. this->setCentralWidget(view);
    15. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance for any insights.

  2. #2
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: co-ordinate confusion: scenePos()

    Hmm well after posting this I saw similar threads where the intuitively appropriate position values are returned if after instantiating the items, setPos() is called. Perhaps the documentation for addRect() etc. should include QGraphicsItem::setPos() in it's "see also".

  3. #3
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: co-ordinate confusion: scenePos()

    Try using boundingRect() -
    Qt Code:
    1. qDebug() << rect1->boundingRect().topLeft()
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: co-ordinate confusion: scenePos()

    Quote Originally Posted by norobro View Post
    Try using boundingRect() -
    Qt Code:
    1. qDebug() << rect1->boundingRect().topLeft()
    To copy to clipboard, switch view to plain text mode 
    Yes, that worked, thank you so much. I was able to instantiate my rectangles and then get meaningful co-ordinate values back from boundingRect() as opposed to rect(), without having to setPos() beforehand. But it is confusing as hell. Why does boundingRect() Do What I Mean but rect() doesn't? Why doesn't an item adopt as its rect(), pos() and scenePos() values the co-ordinates that it is instantiated with (ie., scene->addRect(x, y, side, side)? Why do these things seem so...unintuitive?
    Last edited by Urthas; 15th March 2010 at 03:16.

Similar Threads

  1. ItemAt and ScenePos
    By jano_alex_es in forum Newbie
    Replies: 3
    Last Post: 26th October 2009, 14:48
  2. ScenePos changes when rotating an item
    By jano_alex_es in forum Newbie
    Replies: 2
    Last Post: 23rd October 2009, 08:52
  3. Why does QGraphicsItem::scenePos() return (0,0)
    By extrakun in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2009, 07:35
  4. QGraphicsItem meaning of pos(), scenePos()
    By nicolas1 in forum Qt Programming
    Replies: 11
    Last Post: 9th October 2008, 08:59
  5. QGraphicsItem::scenePos() or pos()
    By Gopala Krishna in forum Newbie
    Replies: 3
    Last Post: 11th February 2007, 19: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.