Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make QMenuBar focused

How to make QMenuBar focused

Scheduled Pinned Locked Moved Unsolved General and Desktop
focusqmenubarqwindow
6 Posts 3 Posters 3.0k Views
  • 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 Offline
    P Offline
    pmendl
    wrote on last edited by
    #1

    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
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • P Offline
        P Offline
        pmendl
        wrote on last edited by
        #3

        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.

        kshegunovK 1 Reply Last reply
        0
        • P pmendl

          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.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @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
          0
          • kshegunovK 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.

            P Offline
            P Offline
            pmendl
            wrote on last edited by
            #5

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

            kshegunovK 1 Reply Last reply
            0
            • P pmendl

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

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

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

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved