Qt-禁用/启用所有快捷方式 [英] Qt - Disable/enable all shortcuts

查看:695
本文介绍了Qt-禁用/启用所有快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有3D视口的Qt 5应用程序,用户在按住RClick并使用WASDQE时可以在其中浏览.我想这样做,因此按住Ctrl可以减慢相机的移动速度,但是这样做会激活快捷方式.

I have a Qt 5 application with a 3D viewport which the user can navigate around when holding RClick and using WASDQE. I'd like to make it so holding Ctrl slows the camera movement down, however doing this will activate shortcuts.

是否可以禁用和启用所有快捷方式,以便在按下鼠标按钮时可以禁用它们?

Is it possible to disable and enable all shortcuts such that I can disable them while mouse buttons are down?

我尝试在主窗口上安装事件过滤器,但是快捷方式仍处于激活状态(尽管每种事件类型都返回true).

I've tried installing an event filter onto my main window however shortcuts are still activated (Despite even returning true for every event type).

推荐答案

我最终为3D视口小部件创建了一个事件过滤器,以检查是否按下了鼠标.每次遇到这些事件(以及按键释放事件)时,我都会在主窗口中调用一个函数(checkShortcutsEnabled()),以根据是否未按下按钮来切换快捷方式内容.

I ended up creating an event filter for my 3D viewport widget to check for mouse presses. Each time I encounter these events (As well as key release events) I then call a function (checkShortcutsEnabled()) on my main window to switch the shortcut content depending on if no buttons are pressed or not.

我还要检查键释放事件的原因是,仅在不按住键盘修饰符的情况下才重新启用快捷键(例如,如果在键盘键之前释放鼠标键,则不会意外触发快捷键)

The reason why I also check for key release events is to only re-enable shortcuts when no keyboard modifiers are held down (Such that if you release your mouse buttons before keyboard keys, you don't accidentally trip a shortcut)

快捷方式默认为Qt::WindowShortcut,这意味着可以在窗口中的任何位置激活它们.当鼠标按钮在视口上方向下移动时,我会暂时将其切换为Qt::WidgetShortcut,这仅意味着如果小部件收到快捷方式(而不是视口小部件,这是我的主窗口的子项),则可以激活它们.这是禁用/重新启用它们的更好的选择,因为我不必修改保存禁用状态,并且工具栏按钮变为灰色.

Shortcuts default to Qt::WindowShortcut, meaning they can be activated anywhere in the window. When mouse buttons are down over the viewport, I switch this tempoararily to Qt::WidgetShortcut, which only means they can be activated if the widget received the shortcuts (But not the viewport widget, which is a child of my main window). It's a nicer alternative to disabling/re-enabling them as I don't have to tinker around with saving disabled state, as well as toolbar buttons going grey.

主窗口类

    class StageEditorWindow : public QMainWindow {
        Q_OBJECT

        friend class ViewportEventFilter;

        protected:
            /**
             * @brief Checks if any mouse buttons are down and disables/enables shortcuts appropriately
             */
            void checkShortcutsEnabled() {
                QList<QAction*> actions = findChildren<QAction*>();

                if (QApplication::mouseButtons() != Qt::NoButton) {
                    for (QAction *a : actions) a->setShortcutContext(Qt::WidgetShortcut);
                } else if (QApplication::keyboardModifiers() == Qt::NoModifier) {
                    //Don't re-enable shortcuts until modifers have been released
                    for (QAction *a : actions) a->setShortcutContext(Qt::WindowShortcut);
                }
            }

        //Don't forget to install the event filter in your constructor

    };

事件过滤器类

    /**
     * @brief Used to check if the mouse is pressed over the viewport and disable shortcuts if so
     */
    class ViewportEventFilter : public QObject {
        Q_OBJECT

        private:
            StageEditorWindow *w;

        public:
            ViewportEventFilter(StageEditorWindow *w, QObject *parent = nullptr) :
                QObject(parent),
                w(w) {}

        protected:
            bool eventFilter(QObject *watched, QEvent *event) {
                if (event->type() == QEvent::MouseButtonPress ||
                        event->type() == QEvent::MouseButtonRelease ||
                        event->type() == QEvent::KeyRelease) {
                    w->checkShortcutsEnabled();
                }

                return QObject::eventFilter(watched, event);
            }
    };

这篇关于Qt-禁用/启用所有快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆