2

I'm trying to implement simple tabbed interface with Qt5. I use QTabWidget with QToolBars placed inside its tabs and I add QActions to the QToolBars.

That works but causes the following issue: any action remains accessible only while its parent tab is active. If I try to use keyboard shortcut for currently "invisible" action, I will have no success. Since there's no menu etc, the tabs are the only place, where the actions are placed.

Here's how I add the elements to the toolbar:

QTabWidget *ribbon               = new QTabWidget(window);
QToolBar *tool_bar_game          = new QToolBar(tab_game);
QAction *action_go_to_next_level = new QAction(window);

action_go_to_next_level->setText(QApplication::translate("Window", "&Next", 0));
action_go_to_next_level->setIcon(QIcon::fromTheme("go-last"));
action_go_to_next_level->setShortcut(QApplication::translate("Window", "PgDown", 0));


ribbon->addTab(tool_bar_game, tr("Game"));
tool_bar_game->addAction(action_go_to_next_level);

and a screenshot:

Ribbon-like 2-tabs interface

How can I make the action accessible with shortcuts, even when the action's parent tab is not currently opened?

Vercetti
  • 437
  • 1
  • 6
  • 17
  • Why don't you move the `QToolBar`s outside of the `QTabBar`? – Georg Schölly Feb 27 '16 at 12:33
  • If I move the toolbars outside tabbar how would I switch between them? – Vercetti Feb 27 '16 at 12:51
  • Can you add a screenshot how it looks like? I'm a bit confused where exactly your QTabBar is. – Georg Schölly Feb 27 '16 at 12:52
  • Here it is: [link](http://postimg.org/image/6lvjkqb3j/) – Vercetti Feb 27 '16 at 13:03
  • And the shortcuts are added by using mnemonic shortcuts, i.e. `toolbar->addAction("&Save")`? – Georg Schölly Feb 27 '16 at 13:18
  • No, I've meant not mnemonic shortcuts but hotkeys. For example: Page Up for "Previous" or Page Down for "Next". They are added when configuring actions, something like: `QTabWidget ribbon = new QTabWidget(window); QToolBar tool_bar_game = new QToolBar(tab_game); QAction *action_go_to_next_level = new QAction(window); ribbon->addTab(tool_bar_game, tr("Game")); tool_bar_game->addAction(action_go_to_next_level); ` (Sorry for single-line code here in comments). – Vercetti Feb 27 '16 at 13:43
  • 1
    @Vercetti, Do you know about [**QtitanRibbon**](http://www.devmachines.com/qtitanribbon-overview.html) library ? – Vladimir Bershov Feb 27 '16 at 13:46
  • @VladimirBershov, thanks, I do. It's non-free, isn't it? – Vercetti Feb 27 '16 at 13:47
  • @Vercetti yes, it is... – Vladimir Bershov Feb 27 '16 at 13:49
  • @VladimirBershov, That's the problem. Also I do not currently need full functionality of MS-style ribbon. Buttons in a single line with icon and, optionally, text under or near it — that would be enough for me. – Vercetti Feb 27 '16 at 13:52
  • @Vercetti: I still don't see how you add the shortcuts currently. I've added the information you gave so far to the question. (Also, I think there is a small mistake, `new QAction()` probably doesn't return a pointer.) – Georg Schölly Feb 27 '16 at 13:59
  • @GeorgSchölly, I've updated the code above. Also new QAction() does return a pointer. – Vercetti Feb 27 '16 at 14:08
  • @Vercetti: Mea culpa, of course. My C++ is a bit rusty. ;-) – Georg Schölly Feb 27 '16 at 14:31

1 Answers1

1

I'm not surprised that this doesn't work, effectively you try to use a shortcut on a hidden widget. It would be very confusing if this worked.

The obvious workaround for this is to add the shortcut instead of to the QAction to a widget that is always active. Personally, I suggest the window.

Without having tested the code, I believe this should work:

QTabWidget *ribbon               = new QTabWidget(window);
QToolBar *tool_bar_game          = new QToolBar(tab_game);
QAction *action_go_to_next_level = new QAction(window);

action_go_to_next_level->setText(QApplication::translate("Window", "&Next", 0));
action_go_to_next_level->setIcon(QIcon::fromTheme("go-last"));

QShortcut *page_down = new QShortcut(QKeySequence("PgDown"), window);
// trigger the action when the shortcut is activated
QObject::connect(page_down,               &QShortcut::activated,
                 action_go_to_next_level, &QAction::trigger);

ribbon->addTab(tool_bar_game, tr("Game"));
tool_bar_game->addAction(action_go_to_next_level);
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267