I have a gra开发者_运维技巧phicsview
and a graphicsscen
, but I don't know how to install and handle the event filter for getting the keyboard events. Can anyone help me with that?
Thanks in advance.
If you have created custom QGraphicsScene class you can just override QWidget's "QWidget::keyPressEvent()" and "QWidget::keyReleaseEvent()" methods.
class MyGraphicsScene : QGraphicsScene
{
void keyPressEvent(QKeyEvent *event);
}
//in cpp
void MyGraphicsScene::keyPressEvent(QKeyEvent *event)
{
// do sth with event
}
If you are just using an istance of QGraphicsScene, you can use parent's keyPressEvent. Whether or not you must give more details
You have two options to do that:
1) Create your own class based on QGraphicsView and override keyPressEvent(). That only has sense if you going to change a lot of other things.
2) Install event filter, using installEventFilter(..) method and pass there filter object which will receive everything you might need
精彩评论