Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

How to activate QActions from keypresses?



  • Does Qt have an API feature for activating QActions when certain keys are pressed, or does each widget need to do this independently via the keyPressEvent()? I know that Java's Swing API has a keymap feature that handles this and was wondering if Qt had something similar.

    In other words - what's the recommended way to have something like Ctrl-V activate a Paste action?



  • @kitfox
    QActions can have "shortcut keys" associated with them. See http://doc.qt.io/qt-5/qaction.html#shortcut-prop. So the action holds what key activates it for you.



  • To complete JonB’s answer, you have several ways to specify a shortcut for an action:

    action->setShortcut(QKeySequence::Copy);
    action->setShortcut(QKeySequence(Qt::CTRL+Qt::key_v));
    action->setShortcut(QKeySequence("CTRL+V");
    

Log in to reply