Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions

QMenu: blocking mouse right button click...



  • Hi!
    I filter the right button click event on QAction button of QMenu by eventFilter...
    But the right button double-clicking is performed...
    How to block that?

    I have a misunderstanding of the event filtering process:
    I don't know why, but if I press twice the right mouse button, the event of the first click will be blocked, but the slot, connected to the triggered signal, will execute.

    ...
    QMenu *menu = new QMenu(this);
    menu->installEventFilter(this);
    QAction *action = new QAction("Button1", this);
    connect(action, SIGNAL(triggered()), this, SLOT(onTriggered()));
    menu->addAction(action);
    menu->popup(...);
    ...
    
    bool MyClass::eventFilter(QObject *obj, QEvent *event)
    {
       bool val = QObject::eventFilter(obj, event); 
       QMenu *menu = dynamic_cast<QMenu*>(obj);
       if(menu && event->type() == QEvent::MouseButtonPress){
          QMouseEvent *mev = dymanic_cast<QMouseEvent*>(event);
          if(mev->button() == Qt::RightButton){
             event->ignore();
             return true;
          }
       }
       return val;
    }
    


  • Some ideas?


  • Lifetime Qt Champion

    Hi
    Try also check event->type() for
    QEvent::MouseButtonDblClick



  • My problem was with popup() method and dynamic creation of QMenu...
    Changed to exec();


Log in to reply