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
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsItem>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
protected:
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow..cpp
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
//QImage image("E:/ori.jpg");
QImage image
("E:/image_00002.bmp");
ui->graphicsView->setScene(scene);
scene->addPixmap(pix);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
ui->graphicsView->setMouseTracking(true);
}
{
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);
ui
->label_X
->setText
(QString::number(x
));
ui
->label_Y
->setText
(QString::number(y
));
ui
->label_Value
->setText
(QString::number(rgbValue
));
}
Re: Show grey level value of pixel in Qt when the mouse is over it
Hi,
Quote:
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
Quote:
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.
Re: Show grey level value of pixel in Qt when the mouse is over it
Quote:
Originally Posted by
^NyAw^
GrayValue = 0.299*R + 0.587*G + 0.114*B
Or just qGray(rgbValue).
Re: Show grey level value of pixel in Qt when the mouse is over it
Quote:
Originally Posted by
wysota
Or just qGray(rgbValue).
This is working as well!!! Thanks
Added after 6 minutes:
Quote:
Originally Posted by
^NyAw^
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
Quote:
#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
Quote:
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));
}
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.
Code:
...
...
void mouseMoveEvent()
{
// your code here
}
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:
Code:
scene->addPixmap(pix);
Have to be something like this:
Code:
MyPixmapItem *item = new MyPixmapItem();
myScene->addItem(item);
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
Code:
{
QPoint local_pt
= mapFromGlobal
(event
->globalPos
());
QPointF img_coord_pt
= mapToScene
(local_pt
);
double x = img_coord_pt.x();
double y = img_coord_pt.y();
emit positionMoved(x, y);
}
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
Code:
ui->graphicsView->setInteractive(false);