Results 1 to 2 of 2

Thread: QGraphicsPixmapItem's boundingRect() pos() different from item's pos() when dragging?

  1. #1
    Join Date
    Jan 2018
    Location
    India, Delhi
    Posts
    10
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Question QGraphicsPixmapItem's boundingRect() pos() different from item's pos() when dragging?

    Hello,
    My application is based on the graphics view framework and so I'm trying to draw some images ( as loaded by user) and allow user to drag them around the scene
    so basically I made a class "AnimatableItem" that inherits from QGraphicsPixmapItem
    and this item has flag set to make it movable.

    I've overrided AnimatableItem:aint() method as follow
    Qt Code:
    1. //override
    2. void AnimatableItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    3. {
    4. painter->setPen(QColor(200,100,50));
    5. painter->drawPixmap(boundingRect().x(), boundingRect().y(), boundingRect().width(), boundingRect().height(), this->pixmap());
    6.  
    7. painter->setBrush(QBrush(QColor(0,0,0)));
    8. painter->drawRect(this->pos().x(), this->pos().y(), this->boundingRect().width(), this->boundingRect().height());
    9. }
    To copy to clipboard, switch view to plain text mode 

    as you see, im drawing a pixmap which apparently seems to be at desired position
    however the 2nd rectangle (black in color) which represents item position ( actual ) is always ahead of boundingRect position and keeps getting farther away as i move more toward
    +x axis and +y axis , how do i make sure item's position and boundingRect() position is same ?
    Last edited by keshav2010; 30th July 2018 at 02:07.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsPixmapItem's boundingRect() pos() different from item's pos() when dragg

    You are dealing with two different coordinate systems here. From the documentation for "The Graphics View Coordinate System":

    Items live in their own local coordinate system. Their coordinates are usually centered around its center point (0, 0), and this is also the center for all transformations. Geometric primitives in the item coordinate system are often referred to as item points, item lines, or item rectangles. ... an item's bounding rect and shape are in item coordinates.
    An item's position is the coordinate of the item's center point in its parent's coordinate system; sometimes referred to as parent coordinates. The scene is in this sense regarded as all parent-less items' "parent". Top level items' position are in scene coordinates.
    For pixmaps, the coordinates are in pixels.

    Any drawing you do should be in item coordinates; the graphics view system will transform these coordinates as needed to translate, rotate, or scale an individual graphics item. The item itself does not need to be aware of how it is transformed when being painted.
    Last edited by d_stranz; 30th July 2018 at 18:07.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    keshav2010 (31st July 2018)

Similar Threads

  1. Aborting dragging event in some area of qml item
    By alizadeh91 in forum Qt Quick
    Replies: 0
    Last Post: 23rd October 2012, 13:41
  2. Replies: 1
    Last Post: 1st April 2011, 15:29
  3. Replies: 0
    Last Post: 2nd July 2010, 15:25
  4. boundingRect()?
    By aaron in forum Qt Programming
    Replies: 6
    Last Post: 21st April 2009, 10:43
  5. Dragging an item from QTableView to a QPushButton
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 23rd May 2007, 12:57

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.