I’m developing a custom widget with QGraphicsScene/View and I have no prior experience with it.
The custom widget is an image viewer that needs to track mouse movement and send signal(s) to it’s parent dialog / window. The signal(s) will be the position of the pixel under the mouse cursor and it’s color (in RGB). A status bar will use that information.
I use a QGraphicsPixmapItem to display the image I load from a file in the scene.
Thanks.
First of all you have to implement the
mouseMoveEventin your custom item. In this function you can easily get the mouse position calling theposfunction. You can get the rgb value if you transform the item’s pixmap into image and call thepixelfunction. You should consider storing theQImageas member variable in order to avoid multiple transformations. Finally you have to emit a custom signal. Sample code follows:Notice that
QGraphicsItemsdo not inherit fromQObjectso by default signals/slots are not supported. You should inherit fromQObjectas well. This is whatQGraphicsObjectdoes. Last but not least I would advise you to enable mouse tracking on yourQGraphicsView