Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsScene: Wrong coordinates returned for position?
Forum Updated to NodeBB v4.3 + New Features

QGraphicsScene: Wrong coordinates returned for position?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 4.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dflorin
    wrote on last edited by
    #1

    I am intrigued by the following behavior when returning the position of a QGraphicsObject

    Here is the code for three cases I tested:

    @QGraphicsScene scene;
    scene.setSceneRect(-(QApplication::desktop()->width()-10)/2, -(QApplication::desktop()->height()-10)/2, QApplication::desktop()->width()-10, QApplication::desktop()->height()-10);

    // 1. This will draw a thick line starting from the center of the screen (at scene position 0,0)
    Wall *topWall = new Wall(QRectF(0, 0, scene.sceneRect().width(), WallThickness));
    qreal xx = topWall->x(); // The value of x is 0
    scene.addItem(topWall);

    // 2. This will draw a thick line at the top of the screen (at pos -315, -175)
    Wall *topWall = new Wall(QRectF(0, 0, scene.sceneRect().width(), WallThickness));
    topWall->setPos(scene.sceneRect().x(), scene.sceneRect().y());
    qreal xx = topWall->x(); // The value of x is -315
    scene.addItem(topWall);

    // 3. This will draw a thick line at the top of the screen (at pos -315, -175)
    Wall *topWall = new Wall(QRectF(scene.sceneRect().x(), scene.sceneRect().y(), scene.sceneRect().width(), WallThickness));
    qreal xx = topWall->x(); // The value of x is 0 !!! Why?
    scene.addItem(topWall);@

    Why is the value of x zero in the 3rd example? I guess the answer would be: because the position is not set. Then why is the line drawn at the right position?

    Just a note:
    scene.sceneRect().x() returns -315
    scene.sceneRect().y() returns -175

    And the implementation of Wall:
    @Wall::Wall(QRectF aRectF, QGraphicsItem *parent)
    : QGraphicsObject(parent)
    , rect(aRectF)
    {
    }

    Wall::~Wall()
    {
    }

    QRectF Wall::boundingRect() const
    {
    qreal adjust = 0.5;
    return QRectF(rect.x() - adjust, rect.y() - adjust, rect.width() + adjust, rect.height() + adjust);
    }

    QPainterPath Wall::shape() const
    {
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
    }

    void Wall::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    QPen pen(niceBlue);
    painter->setPen(pen);
    painter->setBrush(niceBlue);
    painter->drawRect(rect);
    }

    @

    Thanks!

    1 Reply Last reply
    0
    • X Offline
      X Offline
      xsacha
      wrote on last edited by
      #2

      I'm not an expert in this area. You posted in Mobile/Embedded but perhaps it isn't related to mobile?

      It is because the coords are mapped to the wall. When you initialise the Wall with a QRectF, the x and y set the 'top-left' corner of the wall. In the mapping process, this is mapped as the 'origin' (0,0).

      However, if you set top-left to (0,0) as you do in #2, it will be mapped as (0,0) at the top-left corner. So when you change the position to -315,-175, that will be the position of x,y.

      It will still draw at the correct location, only the mapping is changed.

      Read here for further details: http://doc.qt.nokia.com/latest/qrectf.html#QRectF-4

      There is quite a lot of functions in QRectF to move either the mapping (won't affect x) or without mapping (does affect x).

      Hope you understood that :) Tell me if you need clarification.

      • Sacha
      1 Reply Last reply
      0
      • B Offline
        B Offline
        baysmith
        wrote on last edited by
        #3

        [quote author="dflorin" date="1292623469"]Why is the value of x zero in the 3rd example? I guess the answer would be: because the position is not set.[/quote]

        That is correct.

        [quote author="dflorin" date="1292623469"] Then why is the line drawn at the right position?[/quote]

        For the third Wall, you have set the rect to -315, -175. Coordinates for painting of the item are relative to the item's position (0, 0), and therefore match the scene coordinates.

        Nokia Certified Qt Specialist.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dflorin
          wrote on last edited by
          #4

          Got it. Thanks!

          I posted it in Mobile and Embedded as I am developing for Symbian. Actually I could not find a generic, platform independent forum, as this message is not specific to Desktop / Mobile and Embedded. Maybe there should be a Development forum which is not platform oriented.

          Thanks again.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved