PDA

View Full Version : Handling left mouse button event as if it was both left and right



emka
28th August 2010, 15:53
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++).


class MainTab < Qt::TabWidget
def mousePressEvent(event)
if event.button == Qt::RightButton
newEvent = Qt::MouseEvent.new(Qt::Event::MouseButtonPress, event.pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)
mousePressEvent(newEvent)
end
end
end

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.

Urthas
28th August 2010, 17:16
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"?

emka
28th August 2010, 19:25
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.

Urthas
29th August 2010, 17:31
What I meant was the following, given in C++:



void yourClass::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::RightButton) {
...
QTabWidget::mousePressEvent(newEvent); // propagate right as left
}
QTabWidget::mousePressEvent(event); // propagate original event (ie., right as right)
}

emka
29th August 2010, 18:16
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.