[Solved] QGraphisItem Events not called



  • 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?



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

    Tony.



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



  • 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.



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



  • 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.



  • 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.



  • Thank you very much Antonio. It works now.


Log in to reply
 

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