hoverMoveEvent in Qt5 interfering with mousePressEvent

366 Views Asked by At

I have a subclassed QGraphicsPixmapItem (item) set to receive hoverMoveEvents and mousePressEvent. When I click on the item without moving my mouse, the hoverMoveEvent is also triggered. What's more problematic is that when I call QImage::setPixelColor within the hoverMoveEvent, the mousePressEvent is then not triggered. What could be happening?

events.h

class Item : public QGraphicsPixmapItem {
  // constructor code

  protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
}

events.cpp:

Item::Item() {
  this.setAcceptHoverEvents(true);
}

Item::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  QGraphicsItem::mousePressEvent(event);
  event->accept();
  // get x, y pos
  QImage *img = Image::getInstance(); // Global image singleton
  img->setPixelColor(x, y, qRgba(255, 255, 255, 0));
}

Item::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  // get x, y pos
  QImage *img = Image::getInstance();
  img->setPixelColor(x, y, qRgba(0, 0, 0, 0)); --> adding this stops mousePressEvent trigger.
}

I'd like to receive mousepress events together with the hoverevents by editing the image pixels in each event. The hoverEvent handler should not be receiving mouse press events.

0

There are 0 best solutions below