Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Eye Dropper Implementation



  • Hi,
    I need to implement the classic Eye Dropper tool for my application. The canvas is a QGraphicsView object and I wonder if there is a standard algorithm or procedure for this specific feature.
    Understanding that the Eye Dropper is a functionality required by almost any kind of graphic tool, I was expecting to find some example or reference, but I had no luck in my research.

    Any suggestion? Thanks.


  • Lifetime Qt Champion

    Hi,

    Not necessarily the most simple but you could render the widget under the tool to a QImage and then get the color from the pixel position using pixelColor.

    Hope it helps



  • @SGaist Checking the documentation about QWidget: https://doc.qt.io/qt-5/qwidget.html#render
    Would it be possible to render just one pixel of the widget area? In that way, the procedure should be less demanding in terms of memory consumption. What do you think?


  • Lifetime Qt Champion

    Based on the documentation, I think you can accomplish that using the QRegion parameter.



  • Finally, I could find the piece of code I was looking for to implement an eye dropper tool:

    QColor QColorDialogPrivate::grabScreenColor(const QPoint &p)
    {
        const QDesktopWidget *desktop = QApplication::desktop();
        const QPixmap pixmap = QGuiApplication::primaryScreen()->grabWindow(desktop->winId(), p.x(), p.y(), 1, 1);
        QImage i = pixmap.toImage();
        return i.pixel(0, 0);
    }
    

Log in to reply