PDA

View Full Version : QTabWidget: Shortcuts & Clicks



youkai
18th July 2009, 19:50
Hi.

How can I catch a mouse click on a tab that is already the current tab? "currentChanged" is not emitted. :-/

How can I pass shortcuts to widgets in pages that are not visible/selected in the tab widget?

Bye

youkai
18th July 2009, 21:31
I solved the first questions/problem:


MyTabWidget::MyTabWidget(QWidget *parent): QTabWidget(parent) {
tabBar()->installEventFilter(this);
...
}


bool MyTabWidget::eventFilter(QObject *target, QEvent *event) {
if(target == tabBar()) {
if(event->type() == QEvent::MouseButtonPress) {
/* do something here */
}
}
return QTabWidget::eventFilter(target, event);
}


But that shortcut problem is driving me mad. I've tried QShortcut, QAction, button shortcuts, event filters and keypress events ... Waaaaah :D

Bye

fullmetalcoder
18th July 2009, 22:39
But that shortcut problem is driving me mad. I've tried QShortcut, QAction, button shortcuts, event filters and keypress events ... Waaaaah :D

QAction and button shortcuts are only sent to visible widgets so that's not the way to go.

The simplest way (if compatible with your use case) is probably to place the action on the tabwidget and simply redirect the trigger to the proper widget.

Otherwise putting an event filter on the tabwidget (from the widget in the tabs) should do but you'll have to do more processing to get it right(you cannot simply match a key sequence from a key event).

youkai
19th July 2009, 10:04
What kind of event arrives in case of a keyboard shortcut press? I've tried eventfilters to all page widgets already but I didn't find the "hook" event type.

Bye

Edit: Actions, stored in the subclass of QTabWidget will absolutely not recognized if the corresponding shortcut is pressed (key sequence).

Another edit: I will try to catch QEvent::KeyPress in the event filter for all page widgets.

Final edit here: The tabwidget does not get key events at all then, because it has no focus. Damn...

fullmetalcoder
20th July 2009, 08:53
Have you tried changing the shortcutContext (http://doc.trolltech.com/4.3/qaction.html#shortcutContext-prop) of these actions?

youkai
20th July 2009, 11:44
Yes, I have. An application-wide context should do it. But it doesn't work. I'm a little bit confused about using custom shortcuts or actions.

Is it sufficient to store a list of actions in the custom QTabWidget and add them in the constructor like this?


MyTabWidget::MyTabWidget(QWidget *parent): QTabWidget(parent) {
_actions.append(new QAction(this));
_actions.last()->setShortcut(Qt::Key_F1);
_actions.last()->setShortcutContext(Qt::ApplicationShortcut);
connect(_actions.last(), SIGNAL(triggered()), this, SLOT(doSomething()));
}

void MyTabWidget::doSomething() {
...
}


Will "doSomething()" be executed if I hit F1 and the tabwidget has no focus?
Will a shortcut event be triggered to the tabwidget?
Should I use QShortcut instead of QAction?


Bye

fullmetalcoder
20th July 2009, 12:37
Will "doSomething()" be executed if I hit F1 and the tabwidget has no focus?
Will a shortcut event be triggered to the tabwidget?
Should I use QShortcut instead of QAction?




It *should* as far as I understand the docs. However programming is an experimental discipline : test and see for yourself.
I am not sure how shortcut events are born and what they do during their short life but I guess the answer is yes.
Very unlikely. QShortcut is not a class meant to be used directly and I am not sure it will improve things if QAction does not work. In that case the only way would probably be to handle key events directly.

youkai
20th July 2009, 14:12
It *should* as far as I understand the docs. However programming is an experimental discipline : test and see for yourself.
I am not sure how shortcut events are born and what they do during their short life but I guess the answer is yes.
Very unlikely. QShortcut is not a class meant to be used directly and I am not sure it will improve things if QAction does not work. In that case the only way would probably be to handle key events directly.



I tried it - no success. :-( I think there is some kind of central place missing where a key-combo can be checked against present actions. The Qt framework has no idea that I put a list with actions in my tabwidget.
Thanks.
Thanks.


Bye :)

Edit: QWidget::addAction() :o :cool: