Results 1 to 6 of 6

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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 06:53.

Similar Threads

  1. Replies: 6
    Last Post: 2nd March 2018, 04:39
  2. Get QWidget pixel color below mouse Cursor
    By ^NyAw^ in forum Qt Programming
    Replies: 4
    Last Post: 24th May 2010, 17:17
  3. Replies: 0
    Last Post: 7th May 2010, 22:45
  4. Catching Mouse Events on Top-Level Window
    By andyp in forum Qt Programming
    Replies: 6
    Last Post: 8th September 2009, 10:26
  5. Replies: 2
    Last Post: 21st June 2009, 06: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
  •  
Qt is a trademark of The Qt Company.