Skip to content

Handle QMenu sub-menu's and keyboard navigation

Steven Pehrson requested to merge sbp/qttesting:submenu-support into master

What I mean by sub-menu is when one of the QActions in a QMenu is a menuAction.

Keyboard navigation is using keyboard accelearators, or using the arrow keys to navigate the QMenu.

Here is an example widget that demonstrates when these features are needed

class QMenuTester : public QLabel
{
public:
  QMenuTester()
  {
    setText( "Right-click me" );
  }

  void contextMenuEvent( QContextMenuEvent* event ) override
  {
    QMenu submenu( "Sub-Menu 1" );
    submenu.setObjectName( "mSubMenu1" );
    auto action1 = submenu.addAction( "&Action in sub-menu 1" );
    action1->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_0 ));

    QMenu submenu2( "Sub-Menu 2 (added via action)" );
    submenu2.setObjectName( "mSubMenu2FromAction" );
    auto action = submenu2.addAction( "From action added sub-menu" );
    submenu.addAction( submenu2.menuAction() );

    auto submenu2b = submenu.addMenu( "Sub-Menu 2 (Added directly" );
    submenu2b->setObjectName( "mSubMenu2AddedDirectly" );
    submenu2b->addAction( "From directly added sub-menu" );

    QList< QAction* > actions;
    actions.append( new QAction( "Action 1", this ) );
    actions.append( submenu.menuAction() );
    QMenu::exec( actions, event->globalPos() );
  }
};

Merge request reports

Loading