[Moved] taking the value of a pixel from an image

  • Hello,

    I am a beginner in using Qt.I am using a label and a button to display an image. I want to take the x and y coordinates of a pixel using a mousePressEvent().I also use two labels to display my coordinates' values. My program runs correctly, but when I click on the displayed image, I get the coordinates of all the widget and not from the displayed image, with x=0 at down-left point, and y=0 value at up-left point of my window.Could someone help me to get the coordinates from my image and not from entire widget?

    Thanks in advance

    //////////// temp1.h ///
    @#ifndef TEMP1_H
    #define TEMP1_H

    #include <iostream>

    #include <QtGui/QMainWindow>
    #include <QScrollArea>
    #include <QMouseEvent>
    #include <QPoint>
    #include <QTextEdit>
    #include <QLabel>

    #include "ui_temp1.h"

    class temp1 : public QMainWindow, private Ui::temp1Class

    temp1(QWidget *parent = 0, Qt::WFlags flags = 0);

    Ui::temp1Class ui;

    private slots:
    void open_graphicsView(void);

    void temp1::mousePressEvent(QMouseEvent *event);


    #endif // TEMP1_H

    ///// temp1.cpp////

    @#include "temp1.h"

    temp1::temp1(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
    QObject::connect(button, SIGNAL(clicked()), this, SLOT(open_graphicsView()));

    QObject::connect(button, SIGNAL(clicked()), this, SLOT(mousePressEvent()));




    void temp1::open_graphicsView(void)

    QPixmap testImage ("C:/Users/manolis/Desktop/data/test.png");
    label_display_2D->setPixmap(testImage );


    void temp1::mousePressEvent(QMouseEvent *event)

    QPoint pos = mapToParent(event->pos());

    double x = pos.x();
    double y = pos.y();


    ///////// main.cpp///////
    @#include "temp1.h"
    #include <QtGui/QApplication>

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    temp1 w;
    return a.exec();

  • I would use a custom class inheriting from QWidget that has one QImage field, and has a signal that allows you to get the coordinates. Here below are a minimalist example of header and implementation files.

    @class Screen : public QWidget
    Screen( const unsigned& width, const unsigned& height, QWidget * parent = 0 );
    QImage * image;
    void clicked( unsigned& x, unsigned& y );
    private slots:
    void paintEvent( QPaintEvent * );
    void mousePressEvent( QMouseEvent * event );

    @Screen::Screen( const unsigned& width, const unsigned& height, QWidget * parent )
    : QWidget( parent )
    setFixedSize( width, height );
    image = new QImage( width, height, QImage::Format_RGB888 ); // or other format if needed


    void Screen::paintEvent( QPaintEvent * )
    QPainter decorator( this );

    decorator.drawImage( 0, 0, *image );

    void Screen::mousePressEvent( QMouseEvent * event )
    emit clicked( event -> x(), event -> y() );

  • If you have the image in a QLabel and if you do not want to subclass QLabel, simply put an eventFilter on the QLabel in question. If your QLabel is sized exactly as the size of pixmap, then pos at mouse press event should give the pos of the pixel in image.

  • What happens if you simply remove the call to mapToParent()?
    (Also, not so sure this belongs in the C++ Gurus group forum... but not sure how to move it either.)

  • When I make the change from mapFromParent() to mapToParent(), I get the same results and I cannot solve the problem.Is there a new advise?


  • I'm not sure I understand why you are trying to map it to/from the parent widget. What you want to do is map it to the QPixmap - not to/from the parent widget. To map it to the QPixmap, I would guess that you have to subtract an offset, which you should be able to get from the widget that shows the pixmap.

    What are the results you are getting, and how are they compared to what you want?

  • Well, I get the coordinates of all the MainWindow and not from the displayed image (on the label), with x=0 at down-left corner of the MainWindow, and y=0 value at up-left corner of the MainWindow.But, I want my starting point (x,y)=(0,0) to be situated in the down-left corner of the label, used to display the 2D image.

  • The code you posted does not really give a complete picture of your widgets and how they are related. But since you are getting the event in your QMainWindow subclass, you will get them in QMainWindow coordinates. If you use a QLabel to display the image, you should get the event there instead. And the transformation between top-left origin and bottom left-origin is just simple maths - nothing to do with Qt, really. Something like y = height - y.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.