PDA

View Full Version : How to highlight a menu item by program input



horbi
26th July 2013, 14:33
Hi,
I'm working on a piece of software (on Linux/X11), which uses Qt application menus with simulated keyboard inputs and no mouse.

Simulating keyboard inputs works with:


QKeyEvent keyEvent(QEvent::KeyPress,Qt::Key_Tab, Qt::NoModifier);
QWidget* pWidget = qApp->activeWindow()->focusWidget();
qApp->sendEvent(pWidget, &keyEvent);

… for tab input or:


QKeyEvent keyEvent(QEvent::KeyPress,Qt::Key_..., Qt::NoModifier);
QWidget* pWidget = qApp->activePopupWidget();
qApp->sendEvent(pWidget, &keyEvent);

… for other keys

But I always have to use the mouse to get an initial highlighted menu item. I tried a lot of things to get this highlighted item by program input.
With Qmenu::exec() I succeded to open a popup menu but could not get a highlighted item there, so the simulated inputs have no effect.
What am I doing wrong?

Dimanesson
7th August 2015, 09:37
Have you tried to send the KeyRelease event, after sending the KeyPress? For example I used this when I've tried to highlight my QMenu like it's going in Windows:


void MainWindow::keyPressEvent(QKeyEvent* event)
{
if(event->key() == Qt::Key_Alt)
{
if(menuBar()->isHidden())
{
menuBar()->show();
QKeyEvent altPress(QEvent::KeyPress, Qt::Key_Alt, Qt::NoModifier);
QKeyEvent altRelease(QEvent::KeyRelease, Qt::Key_Alt, Qt::NoModifier);

qApp->sendEvent(menuBar(), &altPress);
qApp->sendEvent(menuBar(), &altRelease);
}
}
}