Results 1 to 12 of 12

Thread: QGraphicsItem no mouse events called

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsItem no mouse events called

    Hi!

    I have subclassed QGraphicsItem class in order to create my own custom item. The following are the basic reimplemented functions.

    Qt Code:
    1. QSampleItem::QSampleItem(QPointF p, QGraphicsItem *parent)
    2. : QGraphicsItem(parent),
    3. m_size(QSize(DEFAULT_WIDTH, DEFAULT_HEIGHT))
    4. {
    5. setPos(p);
    6.  
    7. setFlag(QGraphicsItem::ItemIsSelectable, true);
    8. setFlag(QGraphicsItem::ItemIsMovable, true);
    9. setAcceptHoverEvents(true);
    10. setAcceptDrops(true);
    11. }
    12.  
    13. QRectF QSampleItem::boundingRect() const
    14. {
    15. return QRectF(0, 0,
    16. m_size.width() + RESIZE_HANDLE_WIDTH,
    17. m_size.height() + RESIZE_HANDLE_HEIGHT);
    18. }
    19.  
    20. void QSampleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    21. QWidget *widget)
    22. {
    23. Q_UNUSED(option);
    24. Q_UNUSED(widget);
    25.  
    26. painter->drawRoundedRect(RESIZE_HANDLE_WIDTH/2,
    27. RESIZE_HANDLE_HEIGHT/2,
    28. m_size.width(),
    29. m_size.height(),
    30. 5, 5);
    31. }
    To copy to clipboard, switch view to plain text mode 

    I have not re-implemented QGraphicsScene's mouse event functions. In the class above I need to reimplement the mouse event functions so that I can resize the shape from different directions and also move around.

    But the none of following mouse event functions are called in my class.

    Qt Code:
    1. void QSampleItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. qDebug()<<"Inside mousePressEvent";
    4. m_mousePressPoint = event->scenePos().toPoint();
    5.  
    6. QGraphicsItem::mousePressEvent(event);
    7. }
    8.  
    9. void QSampleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    10. {
    11. qDebug()<<"Inside mouseMoveEvent";
    12.  
    13. QGraphicsItem::mouseMoveEvent(event);
    14. }
    15.  
    16. void QSampleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    17. {
    18. m_mousePressPoint = QPoint(-1, -1);
    19.  
    20. QGraphicsItem::mouseReleaseEvent(event);
    21. }
    To copy to clipboard, switch view to plain text mode 

    I have reimplemented the hoverMoveEvent and it works fine.

    Any idea why the mouse events are not called?

    Thanks

  2. #2
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: QGraphicsItem no mouse events called

    Hi,

    I had a similar problem a little time ago. Yes you are right. mouseMoveEvent(..) isn't called at all.( But mousePressEvent should have been called by the way!)

    There are lots of messages regarding this issue in the forum. But if you only need some cosmetic changes, I can suggest you to use QStyle.

    Regards.

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem no mouse events called

    So is this a bug?

  4. #4
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: QGraphicsItem no mouse events called

    I am not expert mate. Maybe there is a particular configuration or setting you need to do. But I tried about 2 weeks to do what you're trying to do. I have also posted many messages regarding this issue but I couldn't acquire the solution of mine.

  5. #5
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem no mouse events called

    Its surprising to see that there is no solution yet to this very basic problem.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QGraphicsItem no mouse events called

    You need to accept() the event in mousePressEvent().
    J-P Nurmi

  7. #7
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: QGraphicsItem no mouse events called

    Hi,

    That means I need to click at least to accept move events? It sounds like kind of ridiculous. Nevertheless, I tried it but it didn't work...

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QGraphicsItem no mouse events called

    No, I mean that in order to receive mouse move events, you must inform the framework that you are interested in receiving them. This is done by accepting the mouse press event. Notice that the default implementation of QGraphicsItem::mousePressEvent() ignores the event. There is a very valid reason to do this. Why deliver tons of move events if the item is not really interested receiving them?
    J-P Nurmi

  9. The following user says thank you to jpn for this useful post:

    advokate3 (8th December 2009)

  10. #9
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem no mouse events called

    Hi.
    Yes, but what if i just want to recieve events when my mouse coursor is over my item. I don't want to press mouse button to check if my coursor is over my item.

    Regards.

  11. #10
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem no mouse events called

    From what I understand, calling event.accept() cancels the "propagation" of the events up the view tree. What if you want to "listen" to the event on your current object but still want the event to keep bubbling up the view hiearchy? is there a way to accomplish that?

  12. #11
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsItem no mouse events called

    Did you enable mouseTracking? Unless this is enabled it won't work (as far as I can say)

  13. #12
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem no mouse events called

    Hmm, no I didn't, but i can't find any methods or properity , that enables mouse tracking. Besides , this probably won't work , becouse :

    If you do receive this event, you can be certain that this item also received a mouse press event,
    So you have to push the button in order to recieve mouse move events. But I don't want that.As I said, I only need to know when my mouse coursor is over my graphicsItem, then emit some signals. Is there any way to accomplish that ?

Similar Threads

  1. QGraphicsItem mouse events
    By zgulser in forum Qt Programming
    Replies: 13
    Last Post: 11th February 2009, 11:19
  2. QGraphicsView Mouse Events
    By tomf in forum Qt Programming
    Replies: 5
    Last Post: 29th July 2008, 15:03
  3. Replies: 9
    Last Post: 22nd June 2008, 22:26
  4. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 06:13
  5. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 19:25

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.