Solved Shortcuts doesn't work on Qt app on OSX
-
I have developed a Qt application on Windows and added some actions to the menu bar in the main window. I have also added some shortcuts for the actions like
Ctrl+O
,Ctrl+S
andCtrl+Q
, as can be seen in the image below.
Now, when I open and run the application on Qt on OSX, the shortcuts were automatically mapped to use Command instead of Control, as you can see in the following image:
The problem is that the shortcuts are not working properly on OSX. The
Command+Q
works fine and closes the application but theCommand+S
andCommand+O
don't work.Any idea what is going on? There is a way or a workaround to solve it?
Step-by-Step
I have created the actions using the Qt Design, so here is a step-by-step to reproduce it:
Create a new project and in the main window add a menu (test1) and an action (test2):
Double-click on the
actionTest2
in the list will open theEdit Action
window where it was added the shortcutCtrl+S
:Now the actionTest2 is triggered by the
Ctrl+S
shortcut:In the triggered slot it was just included a QDebug to find out if it is being called:
void MainWindow::on_actionTest2_triggered() { qDebug() << "Action Test2 Triggered"; }
It works well on Windows and when I press
Ctrl+S
it shows theAction Test2 Triggered
message. Now if I compile and run the application on OSX and tries to pressCtrl+S
orCommand+S
nothing happens.Note: It can also be created by the following code:
QAction *actionOpen = new QAction("Open Action", this); actionOpen->setShortcut(QKeySequence(Qt::Key_O | Qt::CTRL)); addAction(actionOpen); connect(actionOpen, SIGNAL(triggered()), this, SLOT(on_actionTest2_triggered())); QMenu *menu = menuBar()->addMenu(QString(tr("Menu"))); menu->addAction(actionOpen);
I'm using Qt 5.3.
I have found three related questions here, here and here, but I could not find a solution yet.
Based on the answers of these questions I have tried to add the following code to the main window constructor, but it didn't work:
#ifdef Q_OS_MAC ui->actionOpen->setShortcut(QKeySequence(Qt::Key_O | Qt::CTRL)); ui->actionOpen->setShortcutContext(Qt::ApplicationShortcut); #endif
-
Hi,
I did a quick test with:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { QAction *action = new QAction(this); action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O)); connect(action, &QAction::triggered, this, &MainWindow::testMe); addAction(action); } void MainWindow::testMe() { qDebug() << Q_FUNC_INFO; }
and it worked properly.
-
@SGaist, you're right. The example I have shown does not replicate the issue and works fine on OSX.
I have found that the issue was happening because I was setting the
setNativeMenuBar
tofalse
.In my MainWindow constructor I had this line:
ui->menuBar->setNativeMenuBar(false);
I was not using the native menu bar because I had a menu called
Settings
that was not working properly on OSX. Now, I have changed the menu name fromSettings
toTools
and removed theui->menuBar->setNativeMenuBar(false);
command and everything is working fine.So, basically, some shortcuts (e.g.
Command+S
andCommand+O
) doesn't work when setting thesetNativeMenuBar
tofalse
. Probably the OSX uses these shortcuts for other purposes, so the application could not use it.Anyway, thank you very much for the help.
-
Sounds strange... But glad you could get it to work :)