Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QMenu shortcut
Forum Updated to NodeBB v4.3 + New Features

QMenu shortcut

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • yuvaramY Offline
      yuvaramY Offline
      yuvaram
      wrote on last edited by
      #2

      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 Aligeti
      Embedded Qt Developer
      : )

      ? 1 Reply Last reply
      1
      • yuvaramY yuvaram

        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;

        }

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        1
        • yuvaramY Offline
          yuvaramY Offline
          yuvaram
          wrote on last edited by
          #4

          @QString
          dont add "&" as prefix of Date. It will work, shared code is tested.

          Yuvaram Aligeti
          Embedded Qt Developer
          : )

          ? 1 Reply Last reply
          1
          • yuvaramY yuvaram

            @QString
            dont add "&" as prefix of Date. It will work, shared code is tested.

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            @yuvaram Oh, I tested again and it works! I also connected signal, activated() with QMenu's slot, close(). Thanks.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved