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. Why the QAction only added to menu can let the shortcut work?
Forum Updated to NodeBB v4.3 + New Features

Why the QAction only added to menu can let the shortcut work?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.5k 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.
  • L Offline
    L Offline
    ljc123456gogo
    wrote on last edited by
    #1

    HI!
    such as the code I write:

    QAction *m_ACEsc = new QAction(QString("exit"),this);
    m_ACEsc->setShortcut(QString("esc"));
    QObject::connect(m_ACEsc,SIGNAL(triggered()),qApp,SLOT(quit()));
    

    when I press the ESC keyboard, there's no triggered signal emitted.
    only after add to menu :

    menuBar()->addAction(m_ACEsc);
    

    The shortcut working.

    Why? But I don't want the action show in the menu. And I know I can overwrite keyPressEvent to do same thing, but I just want to use QAction do the job. What should I do?
    Thanks in advance!

    Keep Hungery, Keep Foolish!

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      QAction is intended as a visual element. If you just want a shortcut that you can bind to use QShortcut instead.

      L 1 Reply Last reply
      1
      • Chris KawaC Chris Kawa

        QAction is intended as a visual element. If you just want a shortcut that you can bind to use QShortcut instead.

        L Offline
        L Offline
        ljc123456gogo
        wrote on last edited by
        #3

        @Chris-Kawa Thanks very much. You get the point and answer very fast !

        Keep Hungery, Keep Foolish!

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          No problem.
          Btw. avoid strings when not necessary. Instead of QString("esc") you can use Qt::Key_Escape and there's also a connect syntax in Qt5 that does not use strings (via macros):

          QShortcut* sh = new QShortcut(Qt::Key_Escape, this);
          connect(sh, &QShortcut::activated, qApp, &QApplication::quit);
          
          L 1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            No problem.
            Btw. avoid strings when not necessary. Instead of QString("esc") you can use Qt::Key_Escape and there's also a connect syntax in Qt5 that does not use strings (via macros):

            QShortcut* sh = new QShortcut(Qt::Key_Escape, this);
            connect(sh, &QShortcut::activated, qApp, &QApplication::quit);
            
            L Offline
            L Offline
            ljc123456gogo
            wrote on last edited by
            #5

            @Chris-Kawa yeah, you are right. But I use the QString in order to let users to redefine their shortcut, so I didn't use the constant.

            Keep Hungery, Keep Foolish!

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Whenever you let user enter strings you need to validate them. You can use a specialized widget for that: QKeySequenceEdit. It allows you to input a key sequence and stores it in a QKeySequence that you can pass around.

              1 Reply Last reply
              0

              • Login

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