Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to receive gesture events on a QGraphicsItem ?
Qt 6.12 LTS is out! See what's new in the release blog

How to receive gesture events on a QGraphicsItem ?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 4.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          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.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            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 !

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              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( ... );
              };
              @

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt
                • Unsolved