Results 1 to 6 of 6

Thread: Show grey level value of pixel in Qt when the mouse is over it

  1. #1
    Join Date
    Jun 2014
    Posts
    16
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Show grey level value of pixel in Qt when the mouse is over it

    Currently I am working on the display of gray level image with zoom feature. I am able to get the position of the pixel and the zoom feature is working well. However I encountered two problems:

    1.) How can I get the grey level value of the pixel that is pointed by the mouse? I only managed to obtain the rgb value through “QRgb rgbValue = pix.toImage().pixel(x,y)”. How can I convert it into grey level value? Or is there any direct way to get the grey level value of the pixel.

    2.) I have implemented “mouseMoveEvent(QMouseEvent *event)” and “setMouseTracking(true)”. However the function of “mouseMoveEvent(QMouseEvent *event)” is not functioning when I move the mouse. What is wrong with my code?

    mainwindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QGraphicsScene>
    6. #include <QGraphicsItem>
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. private slots:
    21. void on_pushButton_clicked();
    22.  
    23. protected:
    24. void mouseMoveEvent(QMouseEvent * event);
    25.  
    26. private:
    27. Ui::MainWindow *ui;
    28. QPixmap pix;
    29. };
    30.  
    31. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow..cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. //QImage image("E:/ori.jpg");
    10. QImage image("E:/image_00002.bmp");
    11. pix = QPixmap::fromImage(image);
    12. scene = new QGraphicsScene(this);
    13. ui->graphicsView->setScene(scene);
    14. scene->addPixmap(pix);
    15.  
    16.  
    17. }
    18.  
    19. MainWindow::~MainWindow()
    20. {
    21. delete ui;
    22. }
    23.  
    24. void MainWindow::on_pushButton_clicked()
    25. {
    26. ui->graphicsView->setMouseTracking(true);
    27. }
    28.  
    29. void MainWindow::mouseMoveEvent(QMouseEvent *event)
    30. {
    31. QPoint local_pt = ui->graphicsView->mapFromGlobal(event->globalPos());
    32. QPointF img_coord_pt = ui->graphicsView->mapToScene(local_pt);
    33.  
    34. double x = img_coord_pt.x();
    35. double y = img_coord_pt.y();
    36.  
    37. /* How can I get a gray level image here */
    38. QRgb rgbValue = pix.toImage().pixel(x,y);
    39.  
    40. ui->label_X->setText(QString::number(x));
    41. ui->label_Y->setText(QString::number(y));
    42. ui->label_Value->setText(QString::number(rgbValue));
    43. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Show grey level value of pixel in Qt when the mouse is over it

    Hi,

    1.) How can I get the grey level value of the pixel that is pointed by the mouse? I only managed to obtain the rgb value through “QRgb rgbValue = pix.toImage().pixel(x,y)”. How can I convert it into grey level value? Or is there any direct way to get the grey level value of the pixel.
    GrayValue = 0.299*R + 0.587*G + 0.114*B

    2.) I have implemented “mouseMoveEvent(QMouseEvent *event)” and “setMouseTracking(true)”. However the function of “mouseMoveEvent(QMouseEvent *event)” is not functioning when I move the mouse. What is wrong with my code?
    You have to use the "mouseMoveEvent" of the QGraphicsItem instead of MainWindow.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Show grey level value of pixel in Qt when the mouse is over it

    Quote Originally Posted by ^NyAw^ View Post
    GrayValue = 0.299*R + 0.587*G + 0.114*B
    Or just qGray(rgbValue).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following 3 users say thank you to wysota for this useful post:

    benz6699 (24th July 2014), d_stranz (23rd July 2014), ^NyAw^ (23rd July 2014)

  5. #4
    Join Date
    Jun 2014
    Posts
    16
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Show grey level value of pixel in Qt when the mouse is over it

    Quote Originally Posted by wysota View Post
    Or just qGray(rgbValue).
    This is working as well!!! Thanks


    Added after 6 minutes:


    Quote Originally Posted by ^NyAw^ View Post
    Hi,
    GrayValue = 0.299*R + 0.587*G + 0.114*B
    You have to use the "mouseMoveEvent" of the QGraphicsItem instead of MainWindow.
    Thanks a lot. The gray value is working!!!

    However for the "mouseMoveEvent" of the QGraphicsItem I am not very sure how to implement it. I am still very new in Qt. I have tried the following code but failed. It give me the following error messages

    1.) E:\SelfLearning\3D_GUI\ViewZoomIn\mainwindow.cpp:3 2: error: C2027: use of undefined type 'QGraphicsSceneMouseEvent')
    2.) E:\SelfLearning\3D_GUI\ViewZoomIn\mainwindow.cpp:3 2: error: C2227: left of '->globalPos' must point to class/struct/union/generic type

    My code in mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QGraphicsScene>
    #include <QGraphicsItem>
    #include <QGraphicsView>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();


    private slots:
    void on_pushButton_clicked();

    protected:
    //void mousePressEvent(QMouseEvent * event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent * event);

    private:
    Ui::MainWindow *ui;
    QGraphicsScene* scene;
    QGraphicsItem* item;
    QPixmap pix;
    };

    #endif // MAINWINDOW_H
    snippet of code in mainwindow.cpp

    void MainWindow::mouseMoveEvent(QGraphicsSceneMouseEven t *event)
    {
    // QGraphicsItem::mouseMoveEvent(event);
    QPoint local_pt = ui->graphicsView->mapFromGlobal(event->globalPos());
    QPointF img_coord_pt = ui->graphicsView->mapToScene(local_pt);

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

    /* How can I get a gray level image here */
    QRgb rgbValue = pix.toImage().pixel(x,y);
    int greyvalue = qGray(rgbValue);

    ui->label_X->setText(QString::number(x));
    ui->label_Y->setText(QString::number(y));
    ui->label_Value->setText(QString::number(greyvalue));
    }
    Last edited by benz6699; 24th July 2014 at 07:53.

  6. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Show grey level value of pixel in Qt when the mouse is over it

    Hi,

    You have to create a class that inherits from QGraphicsItem(in your case QGraphicsPixmapItem). Then redefine the "mouseMoveEvent" method on this class.
    Qt Code:
    1. class MyPixmapItem : public QGraphicsPixmapItem
    2. ...
    3. ...
    4. void mouseMoveEvent()
    5. {
    6. // your code here
    7. }
    To copy to clipboard, switch view to plain text mode 
    Also you will need to connect some kind of SIGNALs and SLOTs from MyPixmapItem to the main window if you want to show the gray value into a QLabel or QSpinBox(just an example).

    When you add the Item in the scene you have to create an object of this class instead of the base class

    This:
    Qt Code:
    1. scene->addPixmap(pix);
    To copy to clipboard, switch view to plain text mode 

    Have to be something like this:
    Qt Code:
    1. MyPixmapItem *item = new MyPixmapItem();
    2. myScene->addItem(item);
    To copy to clipboard, switch view to plain text mode 
    Òscar Llarch i Galán

  7. The following user says thank you to ^NyAw^ for this useful post:

    benz6699 (30th July 2014)

  8. #6
    Join Date
    Jun 2014
    Posts
    16
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Show grey level value of pixel in Qt when the mouse is over it

    Hello,

    Your suggestion is working. I have create a class CustomView that inherits from QGraphicsView. I have redefined the "mouseMoveEvent" in this class.

    Snipper in CustomView.cpp
    Qt Code:
    1. void CustomView::mouseMoveEvent(QMouseEvent *event)
    2. {
    3. QPoint local_pt = mapFromGlobal(event->globalPos());
    4. QPointF img_coord_pt = mapToScene(local_pt);
    5.  
    6. double x = img_coord_pt.x();
    7. double y = img_coord_pt.y();
    8. emit positionMoved(x, y);
    9. }
    To copy to clipboard, switch view to plain text mode 

    The code above is working perfectly to display the pixel position. However after I redefine the "mouseMoveEvent" in the new class, my panning function is not working anymore. Before that I used the following code in mainwindow to enable panning feature.

    Snippet in mainwindow.cpp

    Qt Code:
    1. ui->graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
    2. ui->graphicsView->setInteractive(false);
    To copy to clipboard, switch view to plain text mode 
    Last edited by benz6699; 30th July 2014 at 12:34. Reason: Wrong code

Similar Threads

  1. Replies: 6
    Last Post: 2nd March 2018, 05:39
  2. Get QWidget pixel color below mouse Cursor
    By ^NyAw^ in forum Qt Programming
    Replies: 4
    Last Post: 24th May 2010, 18:17
  3. Replies: 0
    Last Post: 7th May 2010, 23:45
  4. Catching Mouse Events on Top-Level Window
    By andyp in forum Qt Programming
    Replies: 6
    Last Post: 8th September 2009, 11:26
  5. Replies: 2
    Last Post: 21st June 2009, 07:04

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.