Results 1 to 4 of 4

Thread: mouse events handling with QGraphicsItem

  1. #1
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default mouse events handling with QGraphicsItem

    Hallo all,

    I have a strange problem with the mouse events handling in QGraphicsView and QGraphicsItem.

    I create a:
    "class MyGraphicsView : public QGraphicsView"
    that has a function called:
    "void addButton (tButtonValues& vals);"

    Qt Code:
    1. void MyGraphicsView::addButton(tButtonValues& vals)
    2. {
    3.  
    4. MyButton* btn = new MyButton(vals, MyFrame::GetFrame(vals.Owner));
    5.  
    6. vals.MySelf = btn;
    7. if (vals.Owner < 0) // the button has no owner so it has not been added to the scene yet
    8. m_Scene->addItem(btn);
    9.  
    10. btn->grabMouse();
    11. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that I add 16 buttons to the view but only the last one grabs the mouse.

    In MyButton class I have this method:

    Qt Code:
    1. void MyButton::mouseReleaseEvent ( QGraphicsSceneMouseEvent* event )
    2. {
    3. printf("mouse RELEASE event - <%i> - <%i>\n", event->pos().toPoint().x(), event->pos().toPoint().y() );
    4.  
    5. QRect r( mVals.qpPosition, mVals.qsSize );
    6.  
    7. if ( r.contains(event->pos().toPoint()) )
    8. {
    9. puts("OK");
    10. }
    11. QGraphicsItem::mouseReleaseEvent(event);
    12. }
    To copy to clipboard, switch view to plain text mode 

    I try to propagate the event to all the other items with a mouseReleaseEvent call but no way, only the last created button gets the call.
    Is it a bug or am I doing something wrong ?

    Thanks

  2. #2
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouse events handling with QGraphicsItem

    I am not using grabMouse like you did
    but what I read about that is that on widget at a time can grab the mouse.

    So I am afraid that you can't do what you want that way.

  3. #3
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: mouse events handling with QGraphicsItem

    Quote Originally Posted by scascio View Post
    I am not using grabMouse like you did
    but what I read about that is that on widget at a time can grab the mouse.

    So I am afraid that you can't do what you want that way.
    Yes, I've just found the problem

    grabMouse cannot be used, you're right, but my problem was in boundingRect method.
    I returned:
    Qt Code:
    1. QRectF boundingRect () const { return QRectF(); }
    To copy to clipboard, switch view to plain text mode 
    and this of course means that the buttons are 0 sized.

    Now I return:
    Qt Code:
    1. QRectF boundingRect () const { return QRectF(QPoint(0, 0), mVals.qsSize); }
    To copy to clipboard, switch view to plain text mode 

    and everything works fine

    Thanks anyway.

  4. #4
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: mouse events handling with QGraphicsItem

    I've forgotten on thing:
    If these flags are not set, it doesn't work !

    Qt Code:
    1. setFlags(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsSelectable);
    To copy to clipboard, switch view to plain text mode 
    O at least ItemIsSelectable I think.

Similar Threads

  1. QGraphicsItem no mouse events called
    By munna in forum Qt Programming
    Replies: 11
    Last Post: 9th December 2009, 14:43
  2. QGraphicsItem reimplemented mouse events
    By aarelovich in forum Qt Programming
    Replies: 12
    Last Post: 24th July 2009, 12:56
  3. QGraphicsItem mouse events
    By zgulser in forum Qt Programming
    Replies: 13
    Last Post: 11th February 2009, 11:19
  4. Replies: 9
    Last Post: 22nd June 2008, 22:26
  5. Handling Mouse Events of a parent widget
    By dvmorris in forum Qt Programming
    Replies: 2
    Last Post: 28th March 2007, 18:44

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.