Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Moved] taking the value of a pixel from an image
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 7.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jk_mk
    wrote on last edited by
    #1

    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
    {
    Q_OBJECT

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

    private:
    Ui::temp1Class ui;

    private slots:
    void open_graphicsView(void);

    //////////////
    protected:
    void temp1::mousePressEvent(QMouseEvent *event);

    };

    #endif // TEMP1_H
    @

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

    @#include "temp1.h"

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

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

    }

    temp1::~temp1()
    {

    }

    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();

    label_value_x->setNum(x);
    label_value_y->setNum(y);
    }
    @

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

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

    1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by
      #2

      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
      {
      Q_OBJECT
      public:
      Screen( const unsigned& width, const unsigned& height, QWidget * parent = 0 );
      ~Screen();
      private:
      QImage * image;
      signals:
      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
      }

      Scree::~Screen()
      {}

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

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

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

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      0
      • S Offline
        S Offline
        srikanth_trulyit
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          ludde
          wrote on last edited by
          #4

          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.)

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jk_mk
            wrote on last edited by
            #5

            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?

            Thanks

            1 Reply Last reply
            0
            • L Offline
              L Offline
              ludde
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jk_mk
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  ludde
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved