Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - Prevent menubar from grabbing focus after Alt pressed on Windows

Tags:

c++

windows

qt

In my application, I need to change my mouse cursor and do some stuff differently once the Alt key is pressed, and go back to the normal cursor and normal behavior once the Alt key is released.

Everything works fine on Mac OS, while the Alt-pressing event moves the focus to the menubar on Windows (native Windows behavior), which results in unexpected behaviors of my cursor-changing desire.

So the question is: how to disable this Windows feature (code-wise in Qt of course) and always pass the Alt key press event to the application itself instead of the menubar.

like image 223
Wayee Avatar asked May 04 '16 07:05

Wayee


1 Answers

SH_MenuBar_AltKeyNavigation style hint responsible for menubar selection after Alt pressed. You need to subclass QProxyStyle and override styleHint method like this:

class MenuStyle : public QProxyStyle
{
public:
    int styleHint(StyleHint stylehint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const
    {
        if (stylehint == QStyle::SH_MenuBar_AltKeyNavigation)
            return 0;

        return QProxyStyle::styleHint(stylehint, opt, widget, returnData);
    }
};

Then, set custom style to application.

QApplication a(argc, argv);
a.setStyle(new MenuStyle());
like image 76
Meefte Avatar answered Oct 23 '22 18:10

Meefte