Solved QMenu shortcut
-
Hello, when I make a QMenu and add an action to it with following code, it gets triggered when I press letter 'd'.
QMenu m; m.addAction("&Date",this,SLOT(inserDate())); m.exec(QCursor::pos());
But I want to assign letter '1' as a shortcut. so I tried like below.
QMenu m; m.addAction("Date",this,SLOT(inserDate()),QKeySequence("1")); m.exec(QCursor::pos());
And it gets tirggered only if I click it. When I press number 1, it does not get triggered.
How can I assign QKeySequence to the action? -
Hi @QString
This could help you
#include "mainwindow.h"MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
m_menu = menuBar()->addMenu(tr("&File"));
m_Adate = new QAction(QIcon(":/images/date.png"),tr("Date"),this);
connect(m_Adate,SIGNAL(triggered(bool)),this,SLOT(inserDate()));
m_menu->addAction(m_Adate);
createShortCut();
}void MainWindow::createShortCut()
{
qDebug()<<Q_FUNC_INFO<<endl;
QString keySeqdel = QString("1");
QShortcut *ShortCutdel = new QShortcut(QKeySequence(keySeqdel),this);
connect(ShortCutdel,SIGNAL(activated()),m_Adate,SIGNAL(triggered()),Qt::UniqueConnection);}
MainWindow::~MainWindow()
{}
void MainWindow::inserDate()
{
qDebug()<<Q_FUNC_INFO<<endl;}
-
@yuvaram Thanks for your reply.
But same problem persist.
The stange thing is that it works if I modify name of action from "Data" to "&Date"
But I want to assign letter "1".
Thanks anyway. -
@QString
dont add "&" as prefix of Date. It will work, shared code is tested. -
@yuvaram Oh, I tested again and it works! I also connected signal, activated() with QMenu's slot, close(). Thanks.