How to make QMenuBar focused



  • I hesitate to declare bug in QMenuBar, esspecially if no one is filled for so long time. However...

    I have simple code in simple MainWindow:

    void MainWindow::onShortcutServiceMenuActivated() {
    	qDebug() << "***** Service menu activated";
    	qDebug() << qApp->focusObject()->objectName();
    	menuBar()->show();
    	qDebug() << focusPolicy();
    	menuBar()->setFocus();
    	qDebug() << menuBar()->hasFocus() << isVisible();
    	qDebug() << qApp->focusObject()->objectName();
    	update();
    

    I am getting the supposedly right debug prints:

    ***** Service menu activated
    "lineEdit"
    Qt::FocusPolicy(StrongFocus)
    true true
    "menuBar"
    

    Still no visual change appears in the window. It shows just un-highlited menu bar and does not react on Tab or Up/DownArrow. In contrast, if I click the first menu bar item with mouse, it gets highlited, opens popdown submenu and the above mentioned keys navigates highlite around this sub-menu. This is what I would like to cause by my action instead, without mouse interaction. (Of course everything is done on the active window.)

    What am I doing/understand wrong about setFocus() method?


  • Qt Champions 2016

    Hi
    If you look at the samples with menus.
    http://doc.qt.io/qt-5/qtwidgets-mainwindows-menus-example.html
    Press alt+f to open file menu. then you can navigate with
    keys. up down etc.

    So it seems it can already do this but it must happen via menu items.



  • Thank you for fast reply. Just to make sure by

    So it seems it can already do this but it must happen via menu items.

    you mean, that I have to call setFocus() method on other (menu related) object (which one ?), or that this can be accomplished only by using keyboard?

    The latter is of little help to me, as the application is intended for embedded (Raspberry Pi based) project, where after installation will be no keyboard connected. This is why I intended to emulate basic service menu navigation by programmatic changing focus (to highlight elements) based on reading the joystick (GPIO) port via RaspiComm(TM).

    But you brouth to my mind other possible workaround: is there some chance to inject keyboard events programmaticaly? As I have no experience on this, any clues will be welcome.


  • Qt Champions 2016

    @pmendl

    But you brouth to my mind other possible workaround: is there some chance to inject keyboard events programmaticaly? As I have no experience on this, any clues will be welcome.

    Yes, you can post the event for the appropriate widget through QApplication::postEvent or send it directly by means of QApplication::sendEvent. For example:

    QLabel label;
    QApplication::postEvent(&label,  new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::ShiftModifier));
    

    will post Shift + A key press event for the label widget.



  • @kshegunov Many thanks. I have dived into another problem right now, but will test your guides in about a week and will come back with results.


  • Qt Champions 2016

    @pmendl
    No problem. Do bear in mind that if you decide to send the event with QApplication::sendEvent, you are responsible for the event object's deletion, so it's a good idea to create it on the stack in that case. Good luck!

    Kind regards.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.