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. Repeat calls to "connect" for QShortcut not working
Forum Updated to NodeBB v4.3 + New Features

Repeat calls to "connect" for QShortcut not working

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.0k Views
  • 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.
  • TrayonT Offline
    TrayonT Offline
    Trayon
    wrote on last edited by
    #1

    I made a function to create QShortcuts from a pair consisting of a key and a function to apply when that key is pressed. This way, I can simply declare a vector of pairs and lambda functions to make a clean entry for what I want done. The problem is, my first test works, but my second doesn't. I've recreated a simulation of what I'm trying to see if you guys can spot the problem.

        auto createShortCut3 = [parent](pair<
                QKeySequence, function<void()>
                > kfPair) {
            QShortcut *keyShortcut = new QShortcut(parent);
                keyShortcut->setKey(kfPair.first);
                QObject::connect(keyShortcut, &QShortcut::activated, kfPair.second);
        };
    
        createShortCut3(std::make_pair(Qt::Key_Alt, [this]() {
                qDebug() << "KEY ALT";})); // Doesn't work
    
        createShortCut3(std::make_pair(Qt::Key_Q, [this]() {
                qDebug() << "KEY Q";})); // Works
    
    hskoglundH 1 Reply Last reply
    0
    • TrayonT Trayon

      I made a function to create QShortcuts from a pair consisting of a key and a function to apply when that key is pressed. This way, I can simply declare a vector of pairs and lambda functions to make a clean entry for what I want done. The problem is, my first test works, but my second doesn't. I've recreated a simulation of what I'm trying to see if you guys can spot the problem.

          auto createShortCut3 = [parent](pair<
                  QKeySequence, function<void()>
                  > kfPair) {
              QShortcut *keyShortcut = new QShortcut(parent);
                  keyShortcut->setKey(kfPair.first);
                  QObject::connect(keyShortcut, &QShortcut::activated, kfPair.second);
          };
      
          createShortCut3(std::make_pair(Qt::Key_Alt, [this]() {
                  qDebug() << "KEY ALT";})); // Doesn't work
      
          createShortCut3(std::make_pair(Qt::Key_Q, [this]() {
                  qDebug() << "KEY Q";})); // Works
      
      hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      @Trayon I think it fails because you're trying to use the single Alt key as a shortcut (it's usually reserved by Windows).
      So if you try instead with another letter, say Qt::Key_X, such a repeated call to "connect" should work just fine.

      1 Reply Last reply
      0
      • TrayonT Offline
        TrayonT Offline
        Trayon
        wrote on last edited by
        #3

        Odd, that somehow worked for that particular instance. I'm actually using Linux mint, but it seems it has the same behavior. Where can I read about these particular caveats?

        1 Reply Last reply
        0
        • hskoglundH Offline
          hskoglundH Offline
          hskoglund
          wrote on last edited by
          #4

          Hi, usually you can create a shortcut for the Alt key combined with some digit or letter (even on Windows :-)

          To catch an standalone Alt key being pressed by itsefl (similarly for the Win, Ctrl or Shift keys) you need to go more lowlevel and listen to keypresses.

          1 Reply Last reply
          0
          • TrayonT Offline
            TrayonT Offline
            Trayon
            wrote on last edited by
            #5

            I see. I was using key presses before, but there was a focus error, so I switched over. Thanks for the advice!

            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