c++ - Qt 向 QMainWindow 添加非菜单栏键盘快捷方式

标签 c++ qt keyboard-shortcuts qmainwindow qmenubar

我有一个扩展 QMainWindow 的自定义小部件。我在菜单栏中添加了一些 QAction 以及每个的键盘快捷键,它们工作正常。现在我想从菜单栏中删除其中一些操作,但我想保持快捷方式处于启用状态(用户可以从帮助 对话框中了解快捷方式的可用性)。所以首先我决定让这些 Action 不可见。

那没有用,所以我想如果快捷方式必须起作用,则操作不能不可见。所以我将它添加到主窗口,但它们仍然无法正常工作。任何想法,我如何让它工作?这是我的代码。无论需要发生什么,都在方法 someMethod 中。

class MyWidget: public QMainWindow {
    public:
        MyWidget();

};

MyWidget::MyWidget() {
    QAction *myAct = new QAction(tr("&Some Text"), this);
    fNextmyActPageAct->setShortcut(QKeySequence(Qt::Key_Right));
    myAct->setVisible(false); //adding this does not work
    connect(myAct, SIGNAL(triggered()), this, SLOT(someMethod()));

    ...

    QMenu *someMenu = menuBar()->addMenu(tr("&Some Menu"));
    someMenu->addAction(myAct); //this works, the option shows up in the menu 'Some Menu' and the shortcut works
    this->addAction(myAct); //does not work

}

最佳答案

我测试了这段代码,它工作正常:

QAction* myAct = new QAction(this);
myAct->setShortcut(Qt::Key_Right);
connect(myAct, SIGNAL(triggered()), this, SLOT(someMethod()));
this->addAction(myAct);

不要将 QAction 添加到您的 menuBar。

关于c++ - Qt 向 QMainWindow 添加非菜单栏键盘快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27074722/

相关文章:

c++ - 遇到非法内存访问

C++ 使用 (void*) 转换为 int 进行比较?

c++ - BSD 套接字 ip6 inet_pton 以及如何检索作用​​域 ID

qt - 如何从 QGraphicsScene 生成带有可复制文本的 pdf 文件?

c# - 如何在 Winform 中使用 TextBox 允许 ctrl+a?

eclipse - “Right Click” Eclipse 的键盘快捷键?

c++ - C++ 中的错误,该怎么办?:找不到 ostream::write(long *, unsigned int) 的匹配项

c++ - 包括QT包

windows - Qt4 程序崩溃,除非安装 SDK

Vim Dvorak 键绑定(bind)(重新绑定(bind) :)