PDA

View Full Version : QShortcut not always working



jörg
31st May 2011, 12:35
I use QShortcut inside a widget to close the widget by pressing esc.

shortcut_esc = new QShortcut(this);
shortcut_esc->setKey(Qt::Key_Escape);
connect(shortcut_esc,SIGNAL(activated ()),this,SLOT( myslot() ));

there are 2 differnet ways for a user to open the widget. in the first way everything works fine, the secound the shortcut doesn't work.

in the paint -event of the widget i put a debug-message to detect which widget as the focus:

qDebug() << "focus widget: " << QApplication::focusWidget() << focusWidget() << shortcut_esc->isEnabled() << local->hasFocus() << isActiveWindow() << hasFocus();

the output is the same for both ways, so i think the focus is positioned right.
when the user clicks anywhere in widget, the shortcut works fine even for the secound way.

am i missing something?

where is the difference between clicking on a widget and use the setFocus() Member?

mvuori
31st May 2011, 21:35
there are 2 differnet ways for a user to open the widget. in the first way everything works fine, the secound the shortcut doesn't work.
(...) am i missing something?

Perhaps explaining what the two ways are and what is the way that works and what is the other one that doesn't work.

jörg
1st June 2011, 15:28
i have a list view, the user has the posibility the get a detail-view of an item of the list.

First way:
the user can click on an item to get a detailed-view of the item.
in the detail-view there is the shortcut to leave the detail-view back to the list view by pressing 'ESC' - that works.

Second way:
above the list, there is a button 'Detail-View' that switches the list do the detail of the current item
in the detail-view there is the shortcut to leave the detail-view back to the list view by pressing 'ESC' - that doesn't work.
When i use the second way and just click somewhere in the app, than the shortcut works.

The think i do not understand is that for a shortcut to work, it is important, that the widget with the shortcut has the focus (or on of its childs) and that the toplevel-widget is active.
The detail-view is the same for both ways, and the debug output says that the focused widget are even the same for both ways. So there must be something more importent for the shortcut to work. What is it?

This line in the 'OnPaint'-Member says for both ways the same widgets, shortcut is enabled, has focus, is active
qDebug() << "focus widget: " << QApplication::focusWidget() << focusWidget() << shortcut_esc->isEnabled() << local->hasFocus() << isActiveWindow() << hasFocus();

jörg
8th June 2011, 14:53
no ideas anyone?

nightghost
8th June 2011, 16:51
Do you get a message on the console if you click the shortcut multiple times saying that the shortcut ambiguous?

By the way its really hard to image what you are doing without real code (or at least a shortcut)

jörg
8th June 2011, 17:11
i use the following shortcuts:

shortcut_esc = new QShortcut(this);
shortcut_esc->setKey(Qt::Key_Escape);
connect(shortcut_esc,SIGNAL(activated ()),PlayerExtraScreen::instance(),SLOT( showListView() ));

shortcut_pgup = new QShortcut(this);
shortcut_pgup->setKey(Qt::Key_PageUp);
connect(shortcut_pgup,SIGNAL(activated ()),this,SLOT( backItem() ));
connect(shortcut_pgup,SIGNAL(activatedAmbiguously ()),this,SLOT( backItem() ));

shortcut_pgdown = new QShortcut(this);
shortcut_pgdown->setKey(Qt::Key_PageDown);
connect(shortcut_pgdown,SIGNAL(activated ()),this,SLOT( nextItem() ));
connect(shortcut_pgdown,SIGNAL(activatedAmbiguousl y()),this,SLOT( nextItem() ));

as you can see a also use the Ambigously Signal for testing. but the shortcvut is not ambiuge, i already checked that.
it seems the the key-pressed event is 'swallowed' by an other widget (cause of the fakt that after i clicked somewhere in the app, the shortcut works).
and that is the point wich i do not understand:
the widget is the active widget (the one with the focus) - so why does it get the key event one the first way and not on the second. there must be something beside the attribut 'hasFocus' that the shortcut depends on. what else can it be???? please help

nightghost
8th June 2011, 17:17
What is "this" in the QShortcut Contstructor?

jörg
8th June 2011, 17:59
the code snipped is part of the constructor of the detailview class, whitch is the parent of the shortcut.
so the 'this' pointer is ther instance of the detailview containing the shortcut