[SOLVED] QPushButton right mouse click



  • Hello, I am trying to add menu on pushbutton right mouse click... Code above is not good because it works for clicks everywhere... I just want it to emit rightClick() when pushbutton is clicked with right click...

    @void MainWindow::mousePressEvent(QMouseEvent *event)
    {
    if(event->button() == Qt::RightButton)
    {
    emit rightClick();

    }
    }@

    I also tried with event filter but it does not work... I am doing something wrong... Where do I install that event filter?

    @bool MainWindow::eventFilter(QObject *object, QEvent *event)
    {
    if (object == ui->settingsButton && event->type() == QEvent::MouseButtonPress) {
    QMouseEvent *keyEvent = static_cast<QMouseEvent *>(event);
    if (keyEvent->button() == Qt::RightButton) {
    emit rightClick();
    return true;
    } else
    return false;
    }
    return false;
    }@


  • Moderators

    With the first approach you should subclass QPushButton and override mousePressEvent there, not in the main window that holds the button.

    With the second approach you install the filter on the button object eg.
    @button->installEventFilter(mainWindow);@



  • Solved, thanks...

    @ui->settingsButton->installEventFilter(this);@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.