Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QMenu TAB access through keyboard



  • I have a QMenu inside a QMenuBar as one of the components inside a QMainWindow.

    I want to access the QMenu through keyboard TAB access but the TAB cycles through all the components in the QMainWIndow and skips the QMenu. I tried to set the TAB order in QMainWIndow by calling "setTabOrder" but that didn't work at all.

    Here's my QMenu setup inside the QMainWindow:

    QHBoxLayout *m_pMenuLayout;
    QMenuBar    *m_pMenuBar;
    QMenu       *m_pMenu;
    
    m_pMenuLayout->addWidget(m_pMenuBar);
    m_pMenuLayout->setAlignment(m_pMenuBar, Qt::AlignRight);
    
    m_pMainLayout->addLayout(m_pMenuLayout);
    m_pMainLayout->setAlignment(m_pMenuLayout, Qt::AlignTop);
    

    I tried the following in my QMainWindow class:

    setFocusProxy(m_pMenu);
    setFocusPolicy(Qt::StrongFocus);
    
    setTabOrder(m_pMenu,     m_pButton);
    setTabOrder(m_pButton,   m_pComboBox);
    setTabOrder(m_pComboBox, m_pMenu);
    

    When I do this and press TAB, the focus just cycles back and forth from the QButton to the QComboBox without giving focus to QMenu.


  • Lifetime Qt Champion

    Hi,

    Likely because it's not how menu are usually accessed by keyboards.

    It is usually through accelerators (using the & char) and the alt key.



  • Is there an example of keyboard accelerators you can point me to?

    Is there anyway to override a function to add in TAB into the rotation?

    In my case, the QMenu is just an icon in the top right of my main window. What I want to do is have the QMenu come into focus the first time I press TAB and then cycle through all the components on the screen every subsequent TAB press event...eventually cycling back to the QMenu.

    Is there a possible way to do that?


  • Lifetime Qt Champion


Log in to reply