Mouse Buttons on Mouse Events

You can get mouse buttons like this example:

bool ExampleApplication::eventFilter(QObject *object, QEvent *ev)
{
      if (ev->type() == QEvent::MouseButtonPress)
      {
           QMouseEvent* mouseEvent = (QMouseEvent*)ev;

           if (mouseEvent->button() == Qt::RightButton)
           {
               lbl->setText("Right button pressed!");
           }
           else if (mouseEvent->button() == Qt::LeftButton)
          {
               lbl->setText("Left button pressed!");
          }
     }
     else if (ev->type() == QEvent::MouseButtonRelease)
     {
         QMouseEvent* mouseEvent = (QMouseEvent*)ev;

         if (mouseEvent->button() == Qt::RightButton)
         {
              lbl->setText("Right button released!");
         }
         else if (mouseEvent->button() == Qt::LeftButton)
         {
              lbl->setText("Left button released!");
         }
     }
     return false;
}

Leave a Reply

Your email address will not be published. Required fields are marked *