PDA

View Full Version : handling right click



minomic
13th January 2011, 18:42
Hi everyone, can someone explain me how to handle right clicks, please? I found on the web that you have to use some new classes, but I don't know how to. Please try to be as much precise as you can because I am quite a beginner. Thank you in advance.

high_flyer
13th January 2011, 19:36
Have a look at QWidget::mousePressEvent() (http://doc.trolltech.com/4.7/qwidget.html#mousePressEvent)

minomic
13th January 2011, 20:07
Thank you for your reply. I would like to connect the right button click to a function called function() which is in a class called Class. So what should I exactly write in the code?
I also found this

void mouseReleaseEvent(QMouseEvent* e)
{
if (event->button() & RightButton)
//???
}
Is it ok for what I want to do? And what should I write where there are the question marks? Thank you.

Ceelaz
14th January 2011, 07:27
Hi,

just call your function in the mouseReleaseEvent like this:



void mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() & Qt::RightButton)
{
// call your specific function
this->doSth();
}
}

minomic
14th January 2011, 08:10
Thank you. The thing is that I have many buttons in a QButtonGroup and I want call a function function() on only one of them when I right-click it. Do you know how to do it?

high_flyer
14th January 2011, 08:21
You have two options:
1. Subcalss QPushButton, override the mousePressEvent() or mouseReleaseEvent() based on your needs, and emit a signal from there.
You can then connect that signal to any slot of any other QObject class.
2. Use an event filter in the parent class of the buttons, catch the mousePressEvent (ore released event) and emit a signal form there, which again, you can connect to any other known instance of any other QObject class.