Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 6

Qt-interest Archive, August 2006
[Qt4] Alt-key, focus, QMenuBar


Message 1 in thread

Hi...

How do I prevent my QMainWindow::menuBar() from stealing the focus
when the user presses the Alt-key? There was a thread on this subject
here before back in 2002 which said it there was "no easy way around
it" [1]. Has this guy missed anything or has this been changed in the
meantime.

I just need under some circumstances *every* Alt-key press event to be
received by my widget that has the focus without the focus being lost
in between.

Thanks...

joh

[1] http://lists.trolltech.com/qt-interest/2002-07/thread00325-0.html


--
 [ signature omitted ] 

Message 2 in thread

Johannes Mueller <joh_im_usenet@xxxxxx> skribis:
> 
> How do I prevent my QMainWindow::menuBar() from stealing the focus
> when the user presses the Alt-key? There was a thread on this subject
> here before back in 2002 which said it there was "no easy way around
> it" [1]. Has this guy missed anything or has this been changed in the
> meantime.

I found a solution in the meantime.

For those who are interested:

I reimplemented QWidget::eventFilter() for the widget who needs not to
loose the focus to the menubar under some circumstances to do
something like:


bool MyWidget::eventFilter( QObject*, QEvent* e )
{
        if ( !needToKeepFocus() )
                return false;
        
        switch ( e->type() ) {
            case QEvent::KeyPress:
            case QEvent::KeyRelease:
            case QEvent::ShortcutOverride:
            {
                    QKeyEvent* kev = static_cast<QKeyEvent*>( e );
                    if (kev->key() == Qt::Key_Alt || kev->key() == Qt::Key_Meta) {
                            event( kev );
                            return true;
                    }
            }
            default: break;
        }        
        
        return false;
}

Then i installed MyWidget as an eventFilter of my QApplication by

qApp->installEventFilter(this);

in the constructor of MyWidget.


joh

--
 [ signature omitted ]