Results 1 to 13 of 13

Thread: QGraphicsItem reimplemented mouse events

  1. #1
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsItem reimplemented mouse events

    Hi:
    I need I want implement a basic button because I'm building an application that only uses QGraphicsItems. I need one button so here is the code I wrote to get started:

    Qt Code:
    1. Button::Button(int w, int h, QString cap){
    2. caption = cap;
    3. width = w;
    4. height = h;
    5. color.setRed(40);
    6. color.setGreen(40);
    7. color.setBlue(40);
    8. pen.setColor(color.lighter(130));
    9. pen.setWidth(2);
    10. }
    11.  
    12. QRectF Button::boundingRect() const{
    13. double margin = 2;
    14. return QRectF(0,0,width+margin,height+margin);
    15. }
    16.  
    17. void Button::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    18. Q_UNUSED(widget);
    19. if (option->state & QStyle::State_Sunken){
    20. qDebug() << "I'm being pressed";
    21. painter->setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing);
    22. QBrush brush(color);
    23. painter->setPen(pen);
    24. painter->setBrush(brush);
    25. painter->drawRoundedRect(QRect(0,0,width,height),20.0,20.0,Qt::RelativeSize);
    26. }
    27. else{
    28. painter->setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing);
    29. QBrush brush(color);
    30. painter->setPen(pen);
    31. painter->setBrush(brush);
    32. painter->drawRoundedRect(QRect(0,0,width,height),20.0,20.0,Qt::RelativeSize);
    33. }
    34. }
    35.  
    36. void Button::mousePressEvent(QGraphicsSceneMouseEvent *event){
    37. if (event->button() != Qt::LeftButton){
    38. event->ignore();
    39. return;
    40. }
    41. qDebug() << "press";
    42. QGraphicsItem::mousePressEvent(event);
    43. update();
    44. }
    45.  
    46. void Button::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
    47. qDebug() << "release";
    48. emit(clicked());
    49. QGraphicsItem::mouseReleaseEvent(event);
    50. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that when I press the button all I get is the message "press" and I don't get "release" or the "I'm being pressed" message. I've compared it over and over to the elastic node's example and I can't find any differences that would make mine not work. What's even more bizarre I allready have another implemented class that also uses de the mouseRelease and mousePress events to change cursors and it works perfectly.

    Any one has any idea of what I could be doing wrong?

    Thanks for any help.

  2. #2
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem reimplemented mouse events

    Ok so I did a little bit more of experimenting and I found out the difference between the class I implemented that worked and this one:

    If I use:
    setFlags(ItemIsMovable);

    Then it works. At least the messages appear correctly. Only problem is that the button obviously moves and that is something I really not want.

    Any ideas? thanks.

  3. #3
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem reimplemented mouse events

    So I answered my own question:

    setFlag(ItemIsSelectable)

    That makes all the events work just fine.

    Hope this help someone.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem reimplemented mouse events

    This is incorrect. Button is not selectable so the logic is wrong and making an item selectable shouldn't magically cause it to receive release events.

    Don't call the base class implementation in mousePressEvent() and all should work fine.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem reimplemented mouse events

    Ahhh Ok.

    I knew the button should not be "selectable" but it fixed my problem.
    I'll do this instead

    Thanks anyways, wysota.

    EDIT: Worked like a charm.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem reimplemented mouse events

    Quote Originally Posted by aarelovich View Post
    EDIT: Worked like a charm.
    The real question is why it works after removing the call Or rather why it doesn't work with the call.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem reimplemented mouse events

    Of course.

    I know this should be the question. The thing is I've been workin my butt off with the whole QGraphicsView Framework and I've noticed that is not as "well-oiled" for lack of a better term, as the rest of Qt. Plenty of messages regarding problems with events and such are proof of it. Most of the threads with no satisfactory or 100% satisfactory answer. I was guessing that this is not as "done" as other tools.

    Still, it gets beautifull results and it's pretty useful.

    Now if your question is rhetorical and you actually know the answer, then by all means please tell me so I know.

    Thanks.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem reimplemented mouse events

    Quote Originally Posted by aarelovich View Post
    I know this should be the question. The thing is I've been workin my butt off with the whole QGraphicsView Framework and I've noticed that is not as "well-oiled" for lack of a better term, as the rest of Qt.
    I wouldn't agree. Graphics View is a much more sophisticated and tuned framework than the rest of the toolkit.

    Plenty of messages regarding problems with events and such are proof of it. Most of the threads with no satisfactory or 100% satisfactory answer. I was guessing that this is not as "done" as other tools.
    My guess is that people use it improperly. Of course the architecture has its problems and bugs - it's quite young and develops very dynamically. At the same time it is one of the most popular architectures in Qt so many people abuse it or reveal problems in it.


    Now if your question is rhetorical and you actually know the answer, then by all means please tell me so I know.
    The answer is in the source code of the mousePressEvent routine but it's almost 2AM here so, excuse me, but I'won't be looking there right now. Feel free to do that yourself and report the results here afterwards.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem reimplemented mouse events

    If you say it's ok then I believe you. It's just that I haven't abuse of anything. I wanted to use some of the simpler features and I've gotten several more headaches than when I tried to learn other qt tools. But anyways, there is no way I'm going to look at the mouseEvent code and understand it. But thaks for the encouragment.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem reimplemented mouse events

    Well, I did have a look. It's very educating to solve your problems by looking into Qt's source code, you should try it. Based on the code I can say that if your item doesn't have any flags set (ItemIsMovable or ItemIsSelectable (the others are ignored), to be exact), you shouldn't call the implementation of mousePressEvent from QGraphicsItem because the event will be ignored and forwarded to an item laying below or to the scene which will probably do something that causes incorrect behaviour of mouseReleaseEvent.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem reimplemented mouse events

    Well, that basically fits nicely with why I had to pass the events to the QGraphicsItem::mousePressEvents() in my other class, that Item effectively needed to be moveable.

    I'll try what you tell me some time.

    Thanks.

  12. #12
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem reimplemented mouse events

    So I decided to have a look too. I've searched for the code on the web and found it here:
    http://www.koders.com/cpp/fid93B09A1...+mdef%3Ainsert

    (To be honest I can't find it in Linux)

    I think I see what you mean, but there still there is something that I don't understand.

    I was calling QGraphicsItem::mousePressEvent() in my reimplementation of the mousePress and that event did work, however according to the code here if my item is neither moveable or selectable it should also be ignored, what am I missing?

  13. #13
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem reimplemented mouse events

    Ok, so I've read your original answer and now i get that you've allready answered my own question.

    I'm sorry.

    Thanks.

Similar Threads

  1. QGraphicsItem no mouse events called
    By munna in forum Qt Programming
    Replies: 11
    Last Post: 9th December 2009, 15:43
  2. QGraphicsItem mouse events
    By zgulser in forum Qt Programming
    Replies: 13
    Last Post: 11th February 2009, 12:19
  3. QGraphicsView Mouse Events
    By tomf in forum Qt Programming
    Replies: 5
    Last Post: 29th July 2008, 16:03
  4. Replies: 9
    Last Post: 22nd June 2008, 23:26
  5. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 20: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.