Results 1 to 3 of 3

Thread: Getting pixel coordinates relative to a QGraphicsPolygonItem

  1. #1
    Join Date
    Oct 2011
    Posts
    2
    Qt products
    Qt4

    Question Getting pixel coordinates relative to a QGraphicsPolygonItem

    I am displaying multiple QGraphicsPolygonItem's inside a QGraphicsView. When I hover the mouse inside one of the QGraphicsPolygonItem, I need the pixel position under the mouse relative to the Upper Left Hand Corner of the QGraphicsPolygonItem. I have overloaded the mouseMoveEvent of QGraphicsPolygonItem, and am trying to get the values on mousemove (Code below).

    Qt Code:
    1. void MovablePolygonItemWithHistogram::mouseMoveEvent( QGraphicsSceneMouseEvent * event ){
    2.  
    3. QRectF myBoundingRectangle = this->sceneBoundingRect();
    4. QPoint currPos = this->pos();
    5.  
    6. QPoint currMousePoint = QCursor::pos();
    7. QPointF currMousePointF = this->mapToScene(currMousePoint);
    8. QPointF currEventPoint = event->pos();
    9.  
    10. int xRelative = currMousePointF.x() - myBoundingRectangle.topLeft().x();
    11. int yRelative = currMousePointF.y() - myBoundingRectangle.topLeft().y();
    12. MovableRectangle::mouseMoveEvent(event);
    13. }
    To copy to clipboard, switch view to plain text mode 

    QCursor::pos() gives me the correct mouse coordinates WRT the computer screen. However, myBoundingRectangle always returns the position where I placed it on the parent QGraphicsView. If I try to find location of the QGraphicsPolygonItem using pos() or scenePos() (currPos), it is always 0, 0.

    What should I be doing to get xRelative and yRelative? I read through "The Graphics View Coordinate System" at http://developer.qt.nokia.com/doc/qt...rdinate-system, and am possibly missing something simple here. Would appreciate any pointers.

  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: Getting pixel coordinates relative to a QGraphicsPolygonItem

    Qt Code:
    1. void Item::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
    2. QPointF pos = event->pos() - boundingRect().topLeft();
    3. }
    To copy to clipboard, switch view to plain text mode 
    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.


  3. #3
    Join Date
    Oct 2011
    Posts
    2
    Qt products
    Qt4

    Default Re: Getting pixel coordinates relative to a QGraphicsPolygonItem

    Thanks !! This worked.

Similar Threads

  1. Replies: 6
    Last Post: 2nd March 2018, 04:39
  2. Replies: 1
    Last Post: 15th November 2010, 22:46
  3. Replies: 4
    Last Post: 18th March 2010, 10:11
  4. I need help in QGraphicsPolygonItem
    By c_srikanth1984 in forum Qt Programming
    Replies: 0
    Last Post: 15th April 2009, 10:53
  5. QScrollArea relative viewport coordinates
    By kalanikta in forum Qt Programming
    Replies: 1
    Last Post: 21st April 2006, 10:30

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.