Results 1 to 2 of 2

Thread: Handling mouse events in QGraphicsWidget subclass

  1. #1
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question Handling mouse events in QGraphicsWidget subclass

    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

  2. #2
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Handling mouse events in QGraphicsWidget subclass

    I have since found out the problem only occurs if my Button object is a child of another QGraphicsWidget subclass (ButtonBar).
    However, ButtonBar does not handle any events, or install any event filters. Why should a child of a QGraphicsWidget not receive any events?

Similar Threads

  1. Handling mouse events in Qt WebKit
    By yakin in forum Qt Programming
    Replies: 6
    Last Post: 21st April 2011, 05:44
  2. How to get mouse events from qgraphicswidget
    By newtolinux in forum Qt Programming
    Replies: 4
    Last Post: 21st October 2010, 12:42
  3. Handling mouse events
    By Maluko_Da_Tola in forum Newbie
    Replies: 13
    Last Post: 28th August 2010, 00:37
  4. Handling mouse click events in QGraphicsItem
    By Luc4 in forum Qt Programming
    Replies: 7
    Last Post: 5th March 2010, 16:12
  5. mouse events handling with QGraphicsItem
    By trallallero in forum Qt Programming
    Replies: 3
    Last Post: 21st October 2009, 14:15

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.