Qt Forum

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

    Qt Academy Launch in California!

    Unsolved Managing Alt key press/release events between a menubar and widget

    General and Desktop
    2
    4
    1224
    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.
    • S
      SajasKK last edited by

      The mainwindow of my application has a menubar and a central widget in it. There is an event filter installed on my central widget that is being used to bring in different behaviour on some special key press/release events.

      I am trying to have something similar in case of an Alt Key press/release events.

      But in case of an Alt key press, the focus gets transferred to the menu bar of my widget and the alt key release event is not received by my widgets event filter.

      Here is a short code to demonstrate the issue:

      class MyTextEdit: public QPlainTextEdit
      {
      public:
          MyTextEdit( QWidget * inParent ): QPlainTextEdit( inParent )
          {
              installEventFilter( this );
              setAutoFillBackground(true);
          }
      
          bool eventFilter( QObject * /*inObject*/, QEvent * inEvent )
          {
              switch( inEvent->type() )
              {
              case QEvent::KeyPress:
                  if( static_cast< QKeyEvent * >( inEvent )->key() == Qt::Key_Alt )
                  {
                      setStyleSheet("background-color:black;");
                  }
                  break;
              case QEvent::KeyRelease:
                  if( static_cast< QKeyEvent * >( inEvent )->key() == Qt::Key_Alt )
                  {
                      setStyleSheet("background-color:white;");
                  }
                  break;
              }
              return QPlainTextEdit::event( inEvent );
          }
      };
      
      class MyMenuBar: public QMenuBar
      {
      public:
          MyMenuBar( QWidget * inParent ): QMenuBar( inParent )
          {
              addMenu( "File" );
          }
      
          void keyPressEvent( QKeyEvent * inKeyEvent )
          {
              if( inKeyEvent->key() == Qt::Key_Alt )
                  qDebug()<<"MyMenuBar: AltKey Press";
          }
      
          void keyReleaseEvent( QKeyEvent * inKeyEvent )
          {
              if( inKeyEvent->key() == Qt::Key_Alt )
                  qDebug()<<"MyMenuBar: AltKey Release";
          }
      };
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent)
      {
          setMenuBar( new MyMenuBar( this ) );
          setCentralWidget( new MyTextEdit( this ) );
      }
      

      If I run this code:

      On pressing alt: my text edit background color changes to black.

      On releasing alt: the background color stays as black. Release event goes to menubar

      Would it be possible to have the focus go to the menu bar but to get the release event on the MyTextEditWidget as well.

      That is, I want to let the user navigate the menu bar with alt key press, but I should be able to detect the key release in my central widget(MyTextEdit) even when the focus is on the menubar.

      Is there any other way, than to install the textedit's event filter as the event filter for the menubar?

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

        Hi,

        Are you using accelerators in your menu bar ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        S 1 Reply Last reply Reply Quote 0
        • S
          SajasKK @SGaist last edited by

          @SGaist I don't have any menubar accelerators right now. Currently, on alt press,the focus just moves to the File menu. and if the user press the down arrow key, the menu drops down. I was hoping to keep this behavior.

          And I might use accelerators later.

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

            Alt or AltGr ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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