Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QGraphisItem Events not called
QtWS25 Last Chance

[Solved] QGraphisItem Events not called

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 3.6k Views
  • 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 Offline
    G Offline
    goocreations
    wrote on last edited by
    #1

    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
    0
    • T Offline
      T Offline
      tony
      wrote on last edited by
      #2

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

      Tony.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goocreations
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tony
          wrote on last edited by
          #4

          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
          0
          • G Offline
            G Offline
            goocreations
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tony
              wrote on last edited by
              #6

              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
              0
              • T Offline
                T Offline
                tony
                wrote on last edited by
                #7

                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
                0
                • G Offline
                  G Offline
                  goocreations
                  wrote on last edited by
                  #8

                  Thank you very much Antonio. It works now.

                  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 Extensions
                  • Unsolved