Task: to implement the ability to hide and show the main menu by pressing Alt, as well as highlight the menu after the appearance (such as the FireFox menu also works). Actually, I know how to hide / show, but the menu is always highlighted only by the second Alt press after the menu is shown. I tried to move the focus to menuBar and to a specific menu, tried menuBar()->actions()[0].hover()
, tried to activateWindow()
, setActiveAction()
, activate(QAction::Hover)
We intercept the message on the pressed Alt
bool MainWindow::event(QEvent *event){ if (event->type() == QEvent::KeyPress) { QKeyEvent *ke = static_cast<QKeyEvent*>(event); if (ke->key() == Qt::Key_Alt) { keyReleaseEvent(ke); return true; } } return QMainWindow::event(event); }
Process Alt
void MainWindow::keyReleaseEvent (QKeyEvent* event) { if (event->key() == Qt::Key_Alt){ if (menuBar()->isHidden()){ menuBar()->show(); menuBar()->setFocus(Qt::MenuBarFocusEvent); //Здесь пробовал всё вышеперечисленное } else{ menuBar()->hide(); { } }
qApp->installEventFilter(this)
. In this filter, it handles keystrokes (it does a lot of things in general) and, via Alt, enters the keyboard control mode, which, in fact, means backlighting. The solution was to send a fictitious event about pressing Alt, but in connection with the chemistry described above it is not clear where to send this event. - CerbomenuBar()
. I did it like this:QApplication::sendEvent(menuBar(), altPress);
andQApplication::sendEvent(menuBar(), altRelease);
wherealtPress
andaltRelease
areQKeyEvent
objects - Dimanesson