How to receive gesture events on a QGraphicsItem ?



  • I already set:

    @
    graphicsView->setAttribute(Qt::WA_AcceptTouchEvents);
    graphicsView->viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
    graphicsView->grabGesture(Qt::PinchGesture);
    graphicsView->viewport()->grabGesture(Qt::PinchGesture);
    @

    And I'm still unable to receive gestures on the sceneEvent of a QGraphicsItem using Qt 5.1, does anyone knows why ?


  • Moderators

    you also need to call grabGesture() on the item itself, not only on the qgraphicsview widget.



  • The QGraphicsItem doesn't have the grabGesture() method, that's the problem.


  • Moderators

    right my fault, the grabGesture() is on QGraphicsObject.

    Do you use custom QGraphicsItems? Meaning doing the painting yourself?
    If so you can just derive from QGraphicsObject insteaf of QGraphicsItem.

    Otherwise you would need to wrap your current items with a QGraphicsObject.



  • Thanks for the quick answer raven, I'm not doing the painting myself I'm just using the QGraphicsRectItem as a base class for a class implementing the sceneEvent() method to receive the gestures. What you mean by wrap the items with a QGraphicsObject ? Thanks !


  • Moderators

    hope this works for you (haven't tried it):

    @
    class MyGraphicsObjectWrapper : public QGraphicsObject
    {
    Q_OBJECT

    public:
    MyGraphicsObjectWrapper( QGraphicsItem* item )
    : m_Item(item)
    {
    this->grabGesture( ... );
    m_Item->setParentItem(this);
    }

    virtual QRectF boundingRect () const {  return m_Item->boundingRect(); }
    
    virtual QPainterPath opaqueArea () const {  return m_Item->opaqueArea(); }
    
    virtual QPainterPath shape () const  {  return m_Item->shape(); }
    

    protected:
    virtual bool sceneEvent ( QEvent * event )
    {
    if( event->type() == QEvent::Gesture )
    {
    ....
    }
    return QGraphicsObject::sceneEvent(event);
    }

    QGraphicsItem*   m_Item;
    

    };
    @

    usage:
    @
    QGraphicsRectItem* rectItem( QRectF() );
    scene->addItem( new MyGraphicsObjectWrapper( rectItem ) );
    @

    If that's too much implementation for you you may also go simply with virtual inheritance:
    @
    class MyGraphicsRectItem : public virtual QGraphicsObject, public virtual QGraphicsRectItem
    {
    Q_OBJECT

    public:
    MyGraphicsRectItem( ... );
    };
    @



  • Great thanks Raven, the wrapper worked fine, it was missing only the implementation of the paint() method.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.