Reputation: 3064
Right now I'm able to detect when only one button is clicked. But I want to detect when both are pressed together. The purpose is to select some items from a QTableView. I'm trying to select them in such a way that when left button is clicked on an item while right button is already kept pressed then the item will be among the selected.
The following code only shows the message that the right button is clicked, but it doesn't show that both the buttons are clicked. How can I manage to detect when both of them are clicked?
bool MainWindow::eventFilter(QObject* obj, QEvent *ev)
{
if(obj = ui->listOfImages->viewport())
{
QMouseEvent * mouseEv = static_cast<QMouseEvent*>(ev);
if(mouseEv->buttons() == Qt::RightButton)
{
qDebug()<<"Right Button clicked!";
if(mouseEv->buttons() == Qt::LeftButton)
{
qDebug()<<"Both button clicked!";
return QObject::eventFilter(obj,ev);
}
}
}
return QObject::eventFilter(obj,ev);
}
Thanks.
Upvotes: 1
Views: 2004
Reputation: 121
Try
if( (mouseEv->buttons() & Qt::RightButton) &&
(mouseEv->buttons() & Qt::LeftButton) )
{
...
}
Hint:
When you just checked buttons() is equal to Qt::RightButton, how could it be equal to Qt::LeftButton?
Upvotes: 9
Reputation: 27611
The QMouseEvent::buttons() function returns a combination of OR'd states of the mouse buttons. Therefore, to test the left button is pressed, you should be doing this: -
if(mouseEv->buttons() & Qt::LeftButton)
and for the right button: -
if(mouseEv->buttons() & Qt::RightButton)
As the Qt docs state: -
For mouse press and double click events this includes the button that caused the event. For mouse release events this excludes the button that caused the event.
So you can keep track of when buttons are held down and released.
Upvotes: 7