Tablet Events in the Graphics View Framework
-
Hello,
I am trying to develop a painting application controlled by a wacom tablet, so of course I need support for tablet events. I want to integrate the drawing canvas in the Qt's "Graphics" classes instead of plain QWidget because of the benefits of easy move, zoom, rotate transformations.
The bad is that it seems the Graphics View Framework doesn't support tablet events by default, but my guess is I may be able to add the support by myself.
The question is "how?".Should I install an event filter on the application, detect tablet events and try to integrate them in the framework?
Should I subclass one of Graphics View Framework classes (like QGraphicsView, QGraphicsScene or QGraphicsWidget), capture every event before it is sent to my item in the scene, detect if the event is sent by the tablet (how?) and try to pack them in a QGraphicsSceneEvent subclass?
I appreciate every help or hint.
Thank you -
You can subclass QGraphicsView and check for QTabletEvent and send it to the corresponding item or scene as a custom event. As far as I know it is another kind of QEvent.
-
Thank you for you reply. Yes, this is what I have eventually done. It took me 4 steps:
Subclass QGraphicsView - override event(QEvent *) method, detect tablet events and send them to the scene
@
bool MyGraphicsView::event(QEvent *event)
{
switch (event->type()){
case QEvent::TabletPress:
case QEvent::TabletRelease:
case QEvent::TabletMove:
QApplication::sendEvent(scene(), event);
return true;
default:
return QGraphicsView::event(event);
}
}
@Subclass QGraphicsScene, override event method, detect if the event is a tablet event
@
bool MyGraphicsScene::event(QEvent *event)
{
MyTabletEvent tabletEvent;
switch(event->type()){
case QEvent::TabletPress:
case QEvent::TabletRelease:
case QEvent::TabletMove:
if (static_cast<QTabletEvent>(event)){
tabletEvent = new MyTabletEvent(static_cast<QTabletEvent *>(event), someOtherArgs);
sendEvent(itemAt(tabletEvent->scenePos()), tabletEvent);
return true;
}
return false;
default:
return QGraphicsScene::event(event);
}
}
@Subclass QGraphicsWidget, override event method, and detect mytabletevent
@
bool MyGraphicsWidget::event(QEvent *event)
{
switch(event->type()){
case QEvent::TabletPress:
case QEvent::TabletRelease:
case QEvent::TabletMove:
manageTabletEvent(static_cast<MyTabletEvent *>(event));
return true;
default:
return QGraphicsWidget::event(event);
}
}
@Create a custom event (MyTabletEvent) inherited from QTabletEvent which should be passed between the classes above. It should provied similar API to QGraphicsSceneEvent
Actually this is where I am facing a new problem: how to map coordinates between view->scene->items.
From view->scene I guess mapToScene(viewPos) it's OK, but from scene->items I don't exactly know what to do. Maybe someone helps me figure it out.
Thanks