How to implement QWidgetAction with buttons properly into QMenu

330 Views Asked by At

I'm working on a cross-platform Qt/c++ DJ app with a rather complex QTableView context menu. In order to make the constantly growing menu a bit more compact, I'd like to replace 3 QActions (stacked vertically as usual) with a QwidgetAction that holds 3 buttons in a horizontal layout. I managed to create all that as desired, though I'm having issues with the focus behaviour (keyboard navigation):

  • the QWidgetAction has a focus proxy and receives focus as expected
  • the tab order of the buttons is also set up and working
  • however, once any of the buttons has focus there's no way to escape the button row (= move focus to the QAction above or below)

This is the code:


    m_pAutoDJBtnAction = new QWidgetAction(this);
    
    QWidget* pWidgetAutoDJButtons = new QWidget();
    pWidgetAutoDJButtons->setObjectName("WTrackMenuAutoDJButtons");
    QHBoxLayout* pLayout = new QHBoxLayout();
    
    QLabel* pLabel = new QLabel("Add to AutoDJ queue");
    
    QPushButton* pBtn1 = new QPushButton("Top");
    QPushButton* pBtn2 = new QPushButton("Bottom");
    QPushButton* pBtn3 = new QPushButton("Replace");
    pBtn1->setObjectName("AutoDJAddTopBtn");
    pBtn2->setObjectName("AutoDJAddBottmBtn");
    pBtn3->setObjectName("AutoDJReplaceBtn");
    pBtn1->setFocusPolicy(Qt::StrongFocus);
    pBtn2->setFocusPolicy(Qt::StrongFocus);
    pBtn3->setFocusPolicy(Qt::StrongFocus);
    connect(pBtn1, &QPushButton::clicked, this, &WTrackMenu::slotAddToAutoDJTop);
    connect(pBtn2, &QPushButton::clicked, this, &WTrackMenu::slotAddToAutoDJBottom);
    connect(pBtn3, &QPushButton::clicked, this, &WTrackMenu::slotAddToAutoDJReplace);
    
    pLayout->addWidget(pLabel);
    pLayout->addWidget(pBtn1);
    pLayout->addWidget(pBtn2);
    pLayout->addWidget(pBtn3);
    pWidgetAutoDJButtons->setLayout(pLayout);
    pWidgetAutoDJButtons->setFocusPolicy(Qt::TabFocus);
    pWidgetAutoDJButtons->setFocusProxy(pBtn1);

    m_pAutoDJBtnAction->setDefaultWidget(pWidgetAutoDJButtons);

    addAction(m_pAutoDJBtnAction);

Any idea what I am missing?
Note: Qt version is currently fixed at 5.12 due to some issues on macOS, so I can't tell if that is maybe fixed with a newer version...

0

There are 0 best solutions below