Results 1 to 8 of 8

Thread: How to generate an event for "both the mouse buttons clicked together"?

  1. #1
    Join Date
    Jun 2007
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to generate an event for "both the mouse buttons clicked together"?

    Hi All,

    I have a requirement like following,
    I need to create a box by clicking both the mouse buttons together, hold and drag should create a box on the plot.

    Can you please provide me the solution that I can catch the event for "both the mouse buttons clicked to gether" in Qt? I am not able find this in QEvent or QMouseEvent.

    Thanks,
    Segu

  2. #2
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Wink Re: How to generate an event for "both the mouse buttons clicked together"?

    //QMouseEvent * mouseEvent
    if( (mouseEvent->button() == Qt::LeftButton) &&
    (mouseEvent->button() =Qt::RightButton))
    {
    //your code
    }

    also see this function
    Qt::MouseButtons QMouseEvent::buttons () const


    Hope this helps

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

    Default Re: How to generate an event for "both the mouse buttons clicked together"?

    No, it won't work. button() will return only one of the buttons. Using QMouseEvent::buttons() seems better though.

  4. #4
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: How to generate an event for "both the mouse buttons clicked together"?

    the right click followed by leftclick(vice-versa) where the former is still active .does this not work

    in other words

    if( (mouseEvent->button() == Qt::LeftButton)
    {
    if(mouseEvent->button() == Qt::RightButton))
    {
    //your code
    }
    }
    Last edited by babu198649; 20th November 2007 at 10:36.

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

    Default Re: How to generate an event for "both the mouse buttons clicked together"?

    I don't know what you mean But your code is faulty - you are missing one equality sign ('=') before Qt::RightButton.

  6. #6
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: How to generate an event for "both the mouse buttons clicked together"?

    ok thanks for pointing out error i will try and will post my result i have edited the bug in the previous post.

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

    Default Re: How to generate an event for "both the mouse buttons clicked together"?

    To me the proper way of doing what the thread author wants is:

    Qt Code:
    1. void XXX::mousePressEvent(QMouseEvent *e){
    2. if(e->buttons() & (Qt::LeftButton|Qt::RightButton))
    3. doSomething();
    4. else
    5. baseClass::mousePressEvent(e);
    6. }
    To copy to clipboard, switch view to plain text mode 

    One might also use == instead of & depending on the effect one wants to obtain.

  8. #8
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: How to generate an event for "both the mouse buttons clicked together"?

    i tried u r code it enters if condition even if one button is clicked .but the author of post wants only when both the buttons are active.
    this can be done by

    if(event->buttons() == (Qt::LeftButton | Qt::RightButton))
    code();
    i have tested it and it works

    but one question why do we use or operator and that too unary.

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.