Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [Solved] QGraphisItem Events not called

    General and Desktop
    2
    8
    3279
    Loading More Posts
    • 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.
    • G
      goocreations last edited by

      I've reimplemented QGraphicsItem class:

      @class DistrictItem : public QGraphicsPolygonItem
      {

      public:

      DistrictItem(QGraphicsItem *parent = 0) : QGraphicsPolygonItem(parent)
      {
          setAcceptHoverEvents(true);
      }
      
      void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
      {
          QPoint p[] = {QPoint(0,0), QPoint(50,0), QPoint(50,50)};
          painter->drawPolygon(p, 3);
      }
      
      void hoverMoveEvent ( QGraphicsSceneHoverEvent * )
      {
          cout<<"MouseMove"<<endl;
      }
      

      QRectF boundingRect() const
      {
      return QRectF(0,0,100,100);
      }
      };@

      I use the following code to use my item:

      @QGraphicsScene *scene = new QGraphicsScene();
      DistrictItem *i = new DistrictItem();
      scene->addItem(i);
      QGraphicsView *view = new QGraphicsView(scene);
      view->setMouseTracking(true);
      view->show();@

      The item is successfully created, but when my mouse moves over the shape/item my mouse event is never called (the cout never occurs). I've tried it with other events too, without success.

      Anyone any idea why this doesn't happen?

      1 Reply Last reply Reply Quote 0
      • T
        tony last edited by

        You need to enable mouse tracking for hover events with setAcceptHoverEvents method.

        Tony.

        1 Reply Last reply Reply Quote 0
        • G
          goocreations last edited by

          I've done that in the constructor (line 8)

          1 Reply Last reply Reply Quote 0
          • T
            tony last edited by

            Oops ... I'm really sorry, I didn't see it :(

            What about mouse tracking for the view? Maybe the view is "capturing" the event, cause it's enabled ... anyway I'll do a test right now and I'll check what's wrong ..

            Tony.

            1 Reply Last reply Reply Quote 0
            • G
              goocreations last edited by

              I've also enabled mouse tracking: line 5 from the second part

              1 Reply Last reply Reply Quote 0
              • T
                tony last edited by

                Well, I've found a solution.

                It seems that overriding boundingRect and paint for QGraphicsPolygonItem "spoils" the hover event.

                Infact, if you test this code:

                @
                class DistrictItem : public QGraphicsPolygonItem
                {

                protected:
                void hoverMoveEvent ( QGraphicsSceneHoverEvent * )
                {
                qDebug() <<"MouseMove";
                }

                public:

                DistrictItem(QGraphicsItem *parent = 0) : QGraphicsPolygonItem(parent)
                {
                  QVector< QPointF > points;
                
                  points << QPoint(0,0);
                  points << QPoint(50,0);
                  points << QPoint(50,50);
                
                  setPolygon(points);
                
                  setAcceptHoverEvents(true);
                
                }
                

                };
                @

                @
                QGraphicsScene *scene = new QGraphicsScene;
                scene->addItem(new DistrictItem);
                view->setScene(scene);
                @

                it works. Mouse tracking on the view is useless.

                Tony.

                1 Reply Last reply Reply Quote 0
                • T
                  tony last edited by

                  Ok, the investigation is complete for me :) and now I understand.

                  You chose a polygon item, but you never told it about the "points" that the polygon is made of. Instead you chose to paint it by yourself, and to define its geometry (i.e. boundingRect).

                  The point is that, if you want to do so, you also need to override the "shape" method. The reason why the hover event was not called is that, from the point of view of the polygon item, there was a "null" polygon on the scene, and so no event was to be sent.

                  Infact, if you return to your code, but you implement these three methods

                  @
                  QRectF boundingRect() const
                  {
                  return QRectF(QPoint(0,0),QPoint(50,50));
                  }

                  QPainterPath shape () const
                  {
                    QPainterPath result;
                  
                    result.addRect(QRectF(QPoint(0,0),QPoint(50,50)));
                    return result;
                  }
                  

                  @

                  then you'll receive hover events. Of course I suggest you to define the polygon, cause I think it's the best approach for that. Customizing boundingRect and shape is required for custom shapes, not for regular one.

                  Tony.

                  1 Reply Last reply Reply Quote 0
                  • G
                    goocreations last edited by

                    Thank you very much Antonio. It works now.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post