How to focus menuBar() with Qt

后端 未结 2 930
半阙折子戏
半阙折子戏 2021-01-21 22:38

I have a working application. I added a menuBar() to the main window with some menus. Then, I hid it to free screen space. I wrote the code below so that when user presses ALT k

2条回答
  •  北海茫月
    2021-01-21 23:22

    Have you tried just adding the setFocus command?

    void MainWindow::keyPressEvent( QKeyEvent *k ) {
        if(k->modifiers() & Qt::AltModifier) {
            menuBar()->setHidden(!menuBar()->isHidden());
            menuBar()->setFocus(Qt::MenuBarFocusReason);
            if(menuBar()->hasFocus()) {
                QMessageBox::information(this, "Info", "Focus !");
            }
        }
    }
    

提交回复
热议问题