Reputation: 3613
I'm a beginner to QT. I have used QShortcuts to bind keys to pushbuttons.
QShortcut *sb_0 = new QShortcut(QKeySequence(Qt::Key_0), this);
connect(sb_0, SIGNAL(activated()), ui.b_0, SIGNAL(clicked()));
However, when when b_0 is disabled, the shortcut still works.
ui.b_0->setEnabled(FALSE);
I would like, somehow, that the shortcut be disabled when I disable the pushbutton. I am thinking that I could disconnect the QShortcut from the QPushbutton; however, this doesn't seem like it would be the best idea.
Also, I'm wondering if it'd be possible to have it so that when the key '0' is pressed, the button goes down (gets pushed down), and then when it is released, the button goes back to normal. So basically, pushing '0' is like holiding down left-click on b_0.
If this has already been answered (I couldn't find an answer to this), please let me know.
Thank you.
Upvotes: 1
Views: 1619
Reputation: 8958
Why don't you just call setShortcut on the QPushButton?
void setShortcut ( const QKeySequence & key )
This is provided on the QAbstractButton class, so it might not be the most obvious thing to find in the documentation.
ui.b_0->setShortcut(QKeySequence(Qt::Key_0));
That should do the trick. The button already handles what should happen if the button is not enabled.
Upvotes: 3