PDA

View Full Version : How to send key press events from QGraphicsScene to focused item



sanjayshelke
1st August 2008, 12:33
Hi,

I have a problem in keyPressEvent().

I have a customized scene which contains n numbers of customized QGraphicsItem.

I have reimplemented keyPressEvent() in QCustomScene.

In addition i have also reimplemented keyPressEvent() in my customized item.

In such situation, only QCustomScene keyPressEvent() is called.

Is there any way so that i can send keyPressEvent() to currently focused item.

Regards,
~Sanjay

Benne Gesserit
1st August 2008, 17:50
I think you should call somewhere to setFocus() to the custom item you want to have focus,also set them as fosusable.I think by default an item gets selected if you left click on it ,(if the item is selectable ,of course) ,but it s never given the focus .
So if I m right (I m not sure) ,I would try to catch the event after wich you want the item to has focus .Also dont know if after it s necessary to cal clearFocus() ,but I would say it s not necessary if you can be sure that another object will take the keyboar focus .

Hope it helps .(Actually I hope my answer is not very stupid :D)

patrik08
1st August 2008, 17:58
to becom key press events


void QGraphicsItem::keyPressEvent ( QKeyEvent * event ) [virtual protected]
This event handler, for event event, can be reimplemented to receive key press events for this item. The default implementation ignores the event. If you reimplement this handler, the event will by default be accepted.
Calling QEvent::ignore() or QEvent::accept() on event has no effect.
Note that key events are only received for items that set the ItemIsFocusable flag, and that have keyboard input focus.


set flag QGraphicsItem::ItemIsFocusable and you having keyPressEvent :)




QGraphicsItem::setFlags(this->flags() | QGraphicsItem::ItemIsFocusable );
setFlag(QGraphicsItem::ItemIsMovable,false);