PDA

View Full Version : QGraphicsItem keyPressEvent oddness



jonks
2nd June 2009, 14:40
I'm new to Qt so please be kind!

I've overridden QGraphicsItem::keyPressEvent, focusInEvent and focusOutEvent and set QGraphicsItem::ItemIsFocusable in my QGraphicsItem's constructor.

My objects now receive focusInEvent and focusOutEvent events, but not keyPressEvent.
My scene does not have a keyPressEvent handler, so I know it is not blocking this event from reach my objects.

My project at this stage is too large to post and annoyingly, when I created a minimal project to try to reproduce the problem, keyPressEvent is always received correctly!

What could be blocking keyPressEvent events from getting to my QGraphicsItems?

- I could restart my project putting it back together line by line/function by function (it's about 2000 lines so I'm at the point where doing so will be really annoying). But before I do, if anyone knows of any obvious gotchas that immediately come to mind it would be very muchly appreciated.

Thanks
J

wysota
2nd June 2009, 19:44
Do you call the base class implementation in your handlers?

jonks
3rd June 2009, 07:07
If you mean in focusInEvent, focusOutEvent and keyPressEvent? - then yes.

In my scene I've also just removed all functions and overrides that may affect focus (mouse event handers etc)...but no joy.

nish
3rd June 2009, 07:21
you dont have to tear apart your code to search for keyevent.
just put eventFilter() in your main widget. and see where the keys are going


qApp->installEventFilter(this);
bool MainWidgetThatContainsGrphViewAndItems::eventFilte r(QObject* o,QEvent* e)
{
if(e->type()==QEvent::KeyPress)
{
qWarning()<<"The bad guy which steals the keyevent is"<<o;
}
return false;
}

easy!!

edit: do tell us who was stealing.;)

jonks
3rd June 2009, 13:53
Mr Death - that was pure genius!

I subclassed QGraphicsView and put a keypress handler in there to reset zoom, and forgot to call the base class implementation. :o

lni
3rd June 2009, 15:08
you dont have to tear apart your code to search for keyevent.
just put eventFilter() in your main widget. and see where the keys are going


qApp->installEventFilter(this);
bool MainWidgetThatContainsGrphViewAndItems::eventFilte r(QObject* o,QEvent* e)
{
if(e->type()==QEvent::KeyPress)
{
qWarning()<<"The bad guy which steals the keyevent is"<<o;
}
return false;
}

easy!!

edit: do tell us who was stealing.;)

Just by browsing the thread can teach me a lot even though I don't need it now :)

Thanks!