Results 1 to 5 of 5

Thread: Handling left mouse button event as if it was both left and right

  1. #1
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Handling left mouse button event as if it was both left and right

    Hi,

    so what i want to do is basically to make my QTabWidget able to respond to right mouse button event as if it was a left button and ALSO a right button; tab bar should both set clicked widget as current and exec context menu. The problem is that I have no idea how to achieve that.

    I tried to redefine mousePressEvent as you can see in the example below (code is in Ruby, I hope that's not a problem, but if so I can try to translate it to C++).
    Qt Code:
    1. class MainTab < Qt::TabWidget
    2. def mousePressEvent(event)
    3. if event.button == Qt::RightButton
    4. newEvent = Qt::MouseEvent.new(Qt::Event::MouseButtonPress, event.pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)
    5. mousePressEvent(newEvent)
    6. end
    7. end
    8. end
    To copy to clipboard, switch view to plain text mode 
    Of course it doesn't work, and I am totally clueless what to do now. So I was hoping you could give me some hints.

  2. #2
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Handling left mouse button event as if it was both left and right

    Well, I *think* that the last (non-"end") statement of your function should be Qt::TabWidget::mousePressEvent(event).

    But what exactly do you mean by "doesn't work"?

  3. #3
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Handling left mouse button event as if it was both left and right

    By doesn't work i mean that it acts like I wouldn't change anything at all: tab widget reacts as it is supposed to to the left mouse button, but no reaction whatsoever when I use the right one.

    The "Qt::TabWidget::mousePressEvent(event)" statement won't work, in fact it prevents whole program from running. Probably you ment something else but Ruby syntax made it incorrect, if so please write what the statement you suggested would look like in C++ or whatever language you use.

    And now I realized that I don't call the original function in that posted code, I've changed it already but still no difference. I believe that I must change 'mousePressEvent' in some other widget, but still don't now in which one.

  4. #4
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Handling left mouse button event as if it was both left and right

    What I meant was the following, given in C++:

    Qt Code:
    1. void yourClass::mousePressEvent(QMouseEvent *event)
    2. {
    3. if (event->button() == Qt::RightButton) {
    4. ...
    5. QTabWidget::mousePressEvent(newEvent); // propagate right as left
    6. }
    7. QTabWidget::mousePressEvent(event); // propagate original event (ie., right as right)
    8. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to Urthas for this useful post:

    emka (29th August 2010)

  6. #5
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Handling left mouse button event as if it was both left and right

    Yes, of course you are right, that's not the problem though. Still, thanks for that hint.

    But I figured it out: the mousePressEvent had to be changed not in QTabWidget, but in QTabBar. So I had to make my own child class from QTabBar and reimplement that method there.

  7. The following user says thank you to emka for this useful post:

    Urthas (29th August 2010)

Similar Threads

  1. Qtablewidget left mouse click event in python
    By gerocampo in forum Newbie
    Replies: 0
    Last Post: 23rd July 2010, 18:05
  2. Replies: 8
    Last Post: 10th June 2010, 08:34
  3. Replies: 4
    Last Post: 30th May 2010, 13:55
  4. Emulating Enter key with left mouse click
    By wconstan in forum Newbie
    Replies: 6
    Last Post: 30th December 2009, 17:16
  5. combining Alt + Left Mouse Button
    By speedracer in forum Qt Programming
    Replies: 1
    Last Post: 3rd June 2009, 14:29

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.