#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <cassert>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QAction* back = new QAction(this);
back->setVisible(true);
back->setShortcut(QKeySequence("Ctrl+M"));
bool cres = connect(back, SIGNAL(triggered(bool)), this, SLOT(mySlot()));
assert(cres);
}
In this code I tried to catch Ctrl+M
key event. I don't want to put the action in menu. connect
returns true but mySlot
is never called. When action is inserted in menu, shortcut works well. What I have done wrong?
QShortcut
class directly. – KanalQShortcut
doesn't do anything without the gui. Quoth documentation: "A shortcut is "listened for" by Qt's event loop when the shortcut's parent widget is receiving events." – Frugivorous