What is the fastest way to get QWidget pixel color under mouse?
c++, qt, qt4.8
Solution
The "idiomatic" way of doing this in Qt is completely different from what you're describing. You'd use the Graphics View Framework for this type of thing.
Graphics View provides a surface for managing and interacting with a large number of custom-made 2D graphical items, and a view widget for visualizing the items, with support for zooming and rotation.
You'd define your own `QGraphicsItem` type for the "cells" in the breadboard that would react to hover enter/leave events by changing their color. The connections between the cells (wires, resistors, whatever) would also have their own graphics item types with the features you need for those.
Here's a quick and dirty example for you. It produces a 50x50 grid of green cells that become red when the mouse is over them.
#include <QtGui>
class MyRect: public QGraphicsRectItem
{
public:
MyRect(qreal x, qreal y, qreal w, qreal h)
: QGraphicsRectItem(x,y,w,h) {
setAcceptHoverEvents(true);
setBrush(Qt::green);
}
protected:
void hoverEnterEvent(QGraphicsSceneHoverEvent *) {
setBrush(Qt::red);
update();
}
void hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
setBrush(Qt::green);
update();
}
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QGraphicsScene scene;
for (int i=0; i<50; i++)
for (int j=0; j<50; j++)
scene.addItem(new MyRect(10*i, 10*j, 8, 8));
QGraphicsView view(&scene);
view.show();
return app.exec();
}
You could modify the hover event handlers to talk to your "main window" or "controller" indicating what's currently under the mouse so you can update your caption, legend box or tool palette.
Problem
I need to get the color of pixel under mouse, inside `mouseMoveEvent` of a QWidget (Breadboard). Currently I have this code-> ``` void Breadboard::mouseMoveEvent(QMouseEvent *e) { QPixmap pixmap = QPixmap::grabWindow(winId()); QRgb color = pixmap.toImage().pixel(e->x(), e->y()); if (QColor(color) == terminalColor) QMessageBox::information(this, "Ter", "minal"); } ``` Take a look at (scaled down) screenshot below- When user moves his mouse on breadboard, the hole should get highlighted with some different color (like in red circle). And when the mouse exits, the previous color (grey) should be restored. So I need to do following steps- - Get color under mouse - According to color, floodfill the hole. (Different holes are distinguished using color) - On mouse out, restore the color. There would be wires going over holes, so I can't update the small rectangle (hole) only. What is the fastest way of doing this? My attempt to extract color is not working i.e the Message box in my above code never displays. Moreover I doubt if my existing code is fast enough for my purpose. Remember, how fast you will be moving your mouse on breadboard. Note - I was able to do this using wxWidgets framework. But due to some issues that project got stalled. And I am rewriting it using Qt now. You are invited to look at code https://github.com/vinayak-garg/dic-sim