PDA

View Full Version : Getting pixel coordinates relative to a QGraphicsPolygonItem



neosurya
4th January 2012, 10:48
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).



void MovablePolygonItemWithHistogram::mouseMoveEvent( QGraphicsSceneMouseEvent * event ){

QRectF myBoundingRectangle = this->sceneBoundingRect();
QPoint currPos = this->pos();

QPoint currMousePoint = QCursor::pos();
QPointF currMousePointF = this->mapToScene(currMousePoint);
QPointF currEventPoint = event->pos();

int xRelative = currMousePointF.x() - myBoundingRectangle.topLeft().x();
int yRelative = currMousePointF.y() - myBoundingRectangle.topLeft().y();
MovableRectangle::mouseMoveEvent(event);
}


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-4.8/graphicsview.html#the-graphics-view-coordinate-system, and am possibly missing something simple here. Would appreciate any pointers.

wysota
10th January 2012, 16:14
void Item::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
QPointF pos = event->pos() - boundingRect().topLeft();
}

neosurya
10th January 2012, 23:33
Thanks !! This worked.