1
votes

I'm learning qt by working on given examples. I've started to play a bit with keyboard shortcuts. To assign them I've used QtDesigner which is very handy, for example using "return" key to click a research QPushButton.

Here my main class is a Widget called TextFinder, which has a pointer on a Ui::TextFinder class, which is automatically built from QtCreator, which is the standard procedure to encapsulate user interface attributs and methods.

By assigning a shortcut using QtDesigner, the following lines are generated in the Ui::TextFinder class:

#ifndef QT_NO_SHORTCUT
    findButton->setShortcut(QApplication::translate("TextFinder", "Return", nullptr));
#endif // QT_NO_SHORTCUT

where findButton is an alias for my QPushButton. So far so good.

Suppose now, in a large program, I want to implement many shortcuts to trigger many kinds of signal and I use QtDesigner to do so, it will generate these lines of code, possibly in different header files corresponding to different widgets. It will become quickly difficult to manage them and have a global vision of the "shortcuts state" of the program.

What would be a good method to manage all the shortcuts in the program at one place? Is it possible to make some config file to perform this task?

1

1 Answers

1
votes

Typically in this case you go one level up from the shortcuts and look into Actions. A QAction is a somewhat global (e.g. on the level of a QMainWindow) vehicle for something that the user can trigger (or toggle) through various ways. To name the most prominent ones,

  • Menu entries
  • (Toolbar) buttons
  • Shortcuts

You can manage your actions through Qt Designer (if you have a QMainWindow), or at a central place in your code. Note that the action encompasses not only the shortcut, but also title, icon etc. You can arrange a toolbar in Qt Designer by dragging actions onto it, but you can also manually assign an action to any button in code.