سؤال

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.

هل كانت مفيدة؟

المحلول

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() );
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top