Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved How to make QMenuBar focused

    General and Desktop
    focus qmenubar qwindow
    3
    6
    2439
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      pmendl last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • P
          pmendl last edited by

          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.

          kshegunov 1 Reply Last reply Reply Quote 0
          • kshegunov
            kshegunov Moderators @pmendl last edited by kshegunov

            @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.

            Read and abide by the Qt Code of Conduct

            P 1 Reply Last reply Reply Quote 0
            • P
              pmendl @kshegunov last edited by

              @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.

              kshegunov 1 Reply Last reply Reply Quote 0
              • kshegunov
                kshegunov Moderators @pmendl last edited by

                @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.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post