Search code examples
c++qtkeyboard-shortcutssignals-slotsqkeysequence

qt How to pass a QkeySequence of a qshortcut() to function through connect(Signal,Slot)?


I'm trying to connect several shortcuts to a slot,in order to get their key value and append it to a variable. Something like text input, so i do something like this:

button_1 = new QShortcut::QShortcut(QKeySequence("1"),this);
connect(button_1,SIGNAL(activated(QKeySequence)),this, SLOT(keybord_shortcuts(QKeySequence)));

which is not correct because activated() wont get the sequence that calls the shortcut to my slot keybord_shortcuts.

No such signal QShortcut::activated(QKeySequence)

Is there other way than activated()? Any help welcomed.

Thanks.


Solution

  • Yes, there are no such signal activated(QKeySequence) and you have to connect to the signal activated():

     connect(button_1, SIGNAL(activated()), this, SLOT(keybord_shortcuts()));
    

    But you can get the real shortcut in a slot by using sender():

     void keybord_shortcuts()
     {
          QShortcut* shortcut = qobject_cast<QShortcut*>(sender());
          QKeySequence seq = shortcut->key();
          ...
     }