Repeat calls to "connect" for QShortcut not working

  • 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);
                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

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

  • 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?

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

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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.