3

When I add small image to QGraphicsView, is there a way to get coordinates of image pixel? I have used rubberband for my custom class QGraphicsView but I can only get the QGraphicsView coordinates.

Sgt. Pepper
  • 177
  • 11
  • What exactly do you mean by "get coordinates of image pixel"? Do you mean you want screen coordinates, the position on the image relative to the original image, in pixels, or something else? – TheDarkKnight Dec 12 '13 at 08:44
  • Sorry, now that you remind me, it really is unclear. Actually I need the coordinates with respect to original image, not the graphicsview coordinates. – Sgt. Pepper Dec 12 '13 at 08:52

1 Answers1

2

I guess you want to get pixel from a given position. Assume the given position comes from mouse position.

Say, a class inherited from QGraphicsView:

MyView::MyView( QWidget *parent=0 ) :  QGraphicsView(parent)
{
    setScene( new QGraphicsScene( this ) );
    this->scene()->addPixmap(QPixmap::fromImage(this->image)); // this->image is a QImage
}

Then, implement mouse event

void MyView::mouseMoveEvent( QMouseEvent* event )
{
    QPointF pos =  mapToScene( event->pos() );
    QRgb rgb = this->image.pixel( ( int )pos.x(), ( int )pos.y() );
}
Alston
  • 1,166
  • 2
  • 13
  • 26
  • Thank you. I needed something like that. It works. If I am not asking for more, can rubberband be limited over the image region only? – Sgt. Pepper Dec 12 '13 at 09:14