Hi all,

I have written a QGraphicsWidget subclass that should behave like a simple button. As it is, it should show a pixmap, and emit a signal when the left mouse button is pressed on it.

However, I never receive any mouse events. I've re-implemented the mousePressedEvent, but never reach my breakpoint within this event.

I've seen samples doing similar things that work (e.g. in the AnimatedTile example). I do not see a significant difference, and I'd like to understand what I am doing wrong.

Here's the source code, first the header file:

Qt Code:
  1. #ifndef BUTTON_H
  2. #define BUTTON_H
  3.  
  4. #include <QGraphicsWidget>
  5. #include <QGraphicsPixmapItem>
  6.  
  7. class Button : public QGraphicsWidget
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit Button(QGraphicsItem *parent = 0);
  12. void setPixmap(const QPixmap pixmap);
  13.  
  14. signals:
  15. void pressed(void);
  16.  
  17. public slots:
  18.  
  19. protected:
  20. void mousePressEvent(QGraphicsSceneMouseEvent* event);
  21.  
  22. // Tried my own implementations, but no change
  23. //QRectF boundingRect() const;
  24. //QPainterPath shape() const;
  25.  
  26. private:
  27. QGraphicsPixmapItem* _buttonImage;
  28. };
  29.  
  30. #endif // BUTTON_H
To copy to clipboard, switch view to plain text mode 

Now the cpp file:

Qt Code:
  1. #include "button.h"
  2. #include <QGraphicsSceneMouseEvent>
  3.  
  4. Button::Button(QGraphicsItem *parent) :
  5. QGraphicsWidget(parent, 0)
  6. {
  7. setSizePolicy(QSizePolicy::Fixed,
  8. QSizePolicy::Fixed,
  9. QSizePolicy::DefaultType);
  10. setPreferredSize(0,0);
  11. setAcceptHoverEvents(true);
  12. }
  13.  
  14. void Button::setPixmap(const QPixmap pixmap)
  15. {
  16. if (!_buttonImage)
  17. delete _buttonImage;
  18.  
  19. _buttonImage = new QGraphicsPixmapItem(pixmap, this);
  20. _buttonImage->setPos(0,0);
  21. setPreferredSize(_buttonImage->pixmap().width(),
  22. _buttonImage->pixmap().height());
  23. }
  24.  
  25. void Button::mousePressEvent(QGraphicsSceneMouseEvent* event)
  26. {
  27. if ((event->button() == Qt::LeftButton) &&
  28. (event->modifiers() == Qt::NoModifier))
  29. {
  30. emit pressed();
  31. // Just for testing: Toggle pixmap visibility
  32. _buttonImage->setVisible(!_buttonImage-isVisible());
  33. event->accept();
  34. }
  35. else
  36. QGraphicsWidget::mousePressEvent(event);
  37.  
  38. }
  39.  
  40. /* Tried my own implementation of boundingRect and shape,
  41.   but it did not change anything
  42. QRectF Button::boundingRect() const
  43. {
  44.   return QRectF(0,0,
  45.   _buttonImage->pixmap().width(),
  46.   _buttonImage->pixmap().height());
  47. }
  48.  
  49. QPainterPath Button::shape() const
  50. {
  51.   if (_buttonImage)
  52.   return _buttonImage->shape();
  53.   else
  54.   {
  55.   QPainterPath emptyPath;
  56.   return emptyPath;
  57.   }
  58. }
  59. */
To copy to clipboard, switch view to plain text mode 

Thanks for any hints!

edit: typo