在 Qt 中,如何在菜单中显示键盘快捷键但禁用它们?

发布于 2024-08-14 10:54:57 字数 535 浏览 3 评论 0原文

我正在向主窗口的菜单中添加一堆 QAction。这些操作也可以通过键盘触发,并且我希望快捷方式在菜单中像往常一样可见,例如

-----------------
|Copy     Ctrl+C|
-----------------

我可以使用QAction.setShortcut() 来执行此操作。但是,我希望这些QAction由快捷方式触发;我在其他地方单独处理所有键盘输入。

这可能吗?我可以禁用 QAction 中的快捷方式,但菜单中仍保留快捷方式文本(在此示例中为 Ctrl + C)吗?

编辑:我最终执行此操作的方式是连接到菜单的 aboutToShow()aboutToHide() 事件,并启用/禁用快捷方式因此它们仅在显示菜单时才处于活动状态。但我希望有一个更清洁的解决方案......

I'm adding a bunch of QActions to my main window's menus. These actions can also be triggered by the keyboard, and I want the shortcut to be visible in the menu, as usual, e.g.

-----------------
|Copy     Ctrl+C|
-----------------

I can do this using QAction.setShortcut(). However, I don't want these QActions to be triggered by the shortcuts; I'm handling all keyboard input separately elsewhere.

Is this possible? Can I disable the shortcut in the QAction but still have the shortcut text (in this example Ctrl + C) in my menus?

EDIT: The way I ended up doing it is connecting to the menu's aboutToShow() and aboutToHide() events, and enabling/disabling the shortcuts so they are only active when the menu is shown. But I'd appreciate a cleaner solution...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜光 2024-08-21 10:54:57

您可以继承 QAction 并覆盖 QAction::event(QEvent*):

class TriggerlessShortcutAction : public QAction
{
public:
    ...ctors...

protected:
    virtual bool event(QEvent* e)
    {
        if (e->type() == QEvent::Shortcut)
            return true;
        else
            return QAction::event(e);
    }
};

这将导致发送到您的操作的 QEvent::Shortcut 类型的任何事件都不会触发“triggered()”信号。

You could inherit from QAction and override QAction::event(QEvent*):

class TriggerlessShortcutAction : public QAction
{
public:
    ...ctors...

protected:
    virtual bool event(QEvent* e)
    {
        if (e->type() == QEvent::Shortcut)
            return true;
        else
            return QAction::event(e);
    }
};

This will cause any events of type QEvent::Shortcut sent to your actions to not trigger the 'triggered()' signals.

最单纯的乌龟 2024-08-21 10:54:57
action.setText("Copy\tCtrl+C");

这看起来像是带有快捷方式的操作,但实际上并未安装快捷方式。

这是一个完整的示例:

#include <QtGui>

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    QMainWindow win;

    QMenu *menu = win.menuBar()->addMenu("Test");

    // This action will show Ctrl+T but will not trigger when Ctrl+T is typed.
    QAction *testAction = new QAction("Test\tCtrl+T", &win);
    app.connect(testAction, SIGNAL(triggered(bool)), SLOT(quit()));
    menu->addAction(testAction);

    // This action will show Ctrl+K and will trigger when Ctrl+K is typed.
    QAction *quitAction = new QAction("Quit", &win);
    quitAction->setShortcut(Qt::ControlModifier + Qt::Key_K);
    app.connect(quitAction, SIGNAL(triggered(bool)), SLOT(quit()));
    menu->addAction(quitAction);

    win.show();

    return app.exec();
}
action.setText("Copy\tCtrl+C");

This will look like an action with a shortcut, but the shortcut is not actually installed.

Here is a full example:

#include <QtGui>

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    QMainWindow win;

    QMenu *menu = win.menuBar()->addMenu("Test");

    // This action will show Ctrl+T but will not trigger when Ctrl+T is typed.
    QAction *testAction = new QAction("Test\tCtrl+T", &win);
    app.connect(testAction, SIGNAL(triggered(bool)), SLOT(quit()));
    menu->addAction(testAction);

    // This action will show Ctrl+K and will trigger when Ctrl+K is typed.
    QAction *quitAction = new QAction("Quit", &win);
    quitAction->setShortcut(Qt::ControlModifier + Qt::Key_K);
    app.connect(quitAction, SIGNAL(triggered(bool)), SLOT(quit()));
    menu->addAction(quitAction);

    win.show();

    return app.exec();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文