How to allow user to configure keyboard shortcuts?
-
I'm developing an app with a lot of commands and would like to provide the user with a mechanism to customize the keyboard shortcuts.
I'm having a hard time coming up with any solution that I would call elegant, since all my attempts so far require me to create a new class for each individual action I want to make available (Ie, I'm using ActionInfo to provide info about an action a user can configure, and subclassing ActionRunnable for each different action the user can take):
class ActionInfo : QObject { Q_OBJECT QString _id; QString _name; ActionRunnable* _runnable; QPointer<QAction> _action; ... }
I might be able to replace the _runnable with data I could use to indicate a slot, but I don't know the datatype I should use for this.
Does Qt provide some sort of mechanism to make user configurable shortcuts easier? Any examples of this would be a big help.
-
Just a question. How do you provide mechanism for the user to define the keyboard shorts cuts ? Is it through some configuration file or user enters it ?
-
Just a question. How do you provide mechanism for the user to define the keyboard shorts cuts ? Is it through some configuration file or user enters it ?
@dheerendra I haven't written it yet, but I was thinking that the user could open a dialog box that shows a spreadsheet with two columns - names or ids of commands on the left and shortcuts on the right. I'd also consider using an XML file that maps command id names to keyboard shortcuts.
-
You can use QKeyShort cut and make it happen. It could be some thing like follows.
void MyWidgetShortCuts::on_pushButton_clicked() { QString text = this->ui->lineEdit->text(); QShortcut *shortKey = new QShortcut(QKeySequence(text),this); connect(shortKey,SIGNAL(activated()),this,SLOT(callMe())); }
text is keyboard short cut. You can define your own slot.