Search code examples
qtqtguiqmenuqaction

Enable QMenu access for a disabled QToolButton


I have a QToolButton with an attached QMenu which includes a couple of QActions. One of those actions is the button's default action. The default action dynamically changes when clicking on the actions and this works great.

Now, these QActions get enabled and disabled by signals. When exactly the (current) default action get's disabled, the QToolButton gets disabled, too.

This results in an inaccessible QMenu which still contains enabled QMenu entries (QActions) that I want to be able to select.

So: Can I somehow still make the Menu available when the default action is getting a setEnabled(false)? Or are there some other Ideas?


Solution

  • I did the following to exactly resolve the issue as described:

    In my QToolButton class I override eventFilter as follows:

    bool MultiToolButton::eventFilter( QObject* inObject, QEvent* inEvent )
    {
      MultiToolButton* _multiToolButton = 
        dynamic_cast<MultiToolButton*>(inObject);
      QMouseEvent* _mouseEvent = dynamic_cast<QMouseEvent*>(inEvent); 
    
      if(_multiToolButton 
        && _mouseEvent && _mouseEvent->type() == QEvent::MouseButtonPress)
      {
        mMenu.setEnabled(true);
        showMenu();
        return true;
      }
    
      return QToolButton::eventFilter(inObject, inEvent);
    }
    

    and add the following line to the contructor:

    installEventFilter(this);
    

    Thank you for your answer nonetheless. However, I didn't check it.