I note that this is a very old thread and that no answer at the time was received. However, I am having a very similar issue.
http://www.qtcentre.org/threads/3215...rogramatically
NB: I tried to post the following as a reply and was told i did not have permission to do so. I presume because of the age of the thread.
Most the information I can find seems to imply that setting the focus of an menu pragmatically is not something you would want to do.
The reason I need to do this is its a requirement for our software that both the Alt key and the F10 key should highlight the menubar and allow traversal with arrow keys. I believe this is is to conform with Section 508.
The Alt key as standard provides the functionality I require. It does this both in my Qt Application and in Qt Creator on Windows platform(which is all I care about at this stage).
The F10 key does not do this in Qt Creator however an application like word or outlook will mimic the same behavior of the Alt key if F10 is pressed. This is my goal.
I initially tried to intercept the F10 Key Press and then provide a key event for the Alt key as this would save me having to play with the focus of menus. However that hit a roadblock and I have a ticket with Qt regarding that.
Therefore I am back to trying to program the same behavior that the Alt key provides myself, namely setting focus of the first menu item, which in this case is my file menu, I don't want it to open that menu.
Please Note that I am using Qt 5.5.1 MSVC2013 and Windows 10.
The code I have so far is as follows. However the set focus does not seem to mimic the behavior I want. I want to achieve this without having to use any mouse events if possible.
Does anyhow have any help or advice or are they able to point me in the right direction please.
void NipperStudio::keyPressEvent(QKeyEvent *event)
{
if (event != nullptr)
{
if (event->key() == Qt::Key_F10)
{
menuBar()->setFocus();
for (QObject *obj : menuBar()->children())
{
qDebug() << "4" << obj->objectName();
if (obj->objectName() == "FILEMENU")
{
QMenu *tempMenu = qobject_cast<QMenu*>(obj);
tempMenu->setFocus();
}
}
}
}
}
Bookmarks