Reputation: 177
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.
Upvotes: 3
Views: 2183
Reputation: 1226
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() );
}
Upvotes: 2