Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QPushButton right mouse click
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QPushButton right mouse click

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 8.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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;
    }@

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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);@

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Solved, thanks...

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved