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. Managing Alt key press/release events between a menubar and widget

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.7k 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.
  • S Offline
    S Offline
    SajasKK
    wrote on last edited by
    #1

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

      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
      0
      • SGaistS SGaist

        Hi,

        Are you using accelerators in your menu bar ?

        S Offline
        S Offline
        SajasKK
        wrote on last edited by
        #3

        @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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0

          • Login

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