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