First of all you have to implement the mouseMoveEvent
in your custom item. In this function you can easily get the mouse position calling the pos
function. You can get the rgb value if you transform the item's pixmap into image and call the pixel
function. You should consider storing the QImage
as member variable in order to avoid multiple transformations. Finally you have to emit a custom signal. Sample code follows:
void MyPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
{
QPointF mousePosition = event->pos();
QRgb rgbValue = pixmap().toImage().pixel(mousePosition.x(), mousePosition.y());
emit currentPositionRgbChanged(mousePosition, rgbValue);
}
Notice that QGraphicsItems
do not inherit from QObject
so by default signals/slots are not supported. You should inherit from QObject
as well. This is what QGraphicsObject
does. Last but not least I would advise you to enable mouse tracking on your QGraphicsView