The “idiomatic” way to do this in Qt is completely different from what you are describing. You are using the Graphics View Framework for this type of thing.
The graphic view provides a surface for managing and interacting with a large number of customizable 2D graphic elements and a view widget for visualizing elements with support for scaling and rotation.
You define your own QGraphicsItem type for the "cells" in the layout, which will respond to freeze input / stop events by changing their color. Connections between cells (wires, resistors, etc.) will also have their own types of graphic elements with the functions you need for them.
Here's a quick and dirty example. It creates a grid of green 50 × 50 cells that turn 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 can change the hover event handlers to talk to your “main window” or “controller” indicating what is currently under the mouse so that you can update the title, legend box, or tool palette.
source share