Search code examples
c++qtqgraphicsviewqgraphicssceneqpainter

`paint` method isn't called after `QGraphicsView` scroll


I have a QGraphicsItem on scene. hoverEnterEvent is reimplemented for this item. This is the code from hoverEnterEvent:

grabber = new agnt::A_CornerGrabber(A_CornerGrabber::TOP_RIGHT,this,8,3);
assert(grabber);
grabber->installSceneEventFilter(this);
update(boundingRect());

A_CornerGrabber is a class inherited from QGraphicsRectItem. The idea is when you hover item a grabber appears so you can drag it to resize main item.

It works OK until I scroll graphicsview so, that first item is not whole in the view. And afetr this hoverEnterEvent still called, grabber ctor still called, but grabber->paint() method isnt called anymore. Here is grabber->paint():

    painter->setPen(Qt::NoPen);
    QRectF pRect  = parent->boundingRect();
    painter->drawRect(rect());
    setPos(pRect.bottomRight().x()-size-adjust,pRect.bottomRight().y()-size-adjust);

Why is that?

P.S. Everything worked fine some time ago before I made some changes to my code. Unfortunately I can't restore old code and I cant understand what exactly spoiled grabber->paint().

EDIT: added sceneEventFilter()

if(event->type()== 155)
{
    QGraphicsSceneMouseEvent* me = dynamic_cast<QGraphicsSceneMouseEvent*>(event);
    if(!me) return true;
    if(rect().adjusted(0,0,me->pos().x()-me->lastPos().x(),0).width()>xStep)
    {
        prepareGeometryChange();
        int relX = me->pos().x() - xOffset;
        int newRelX = me->lastPos().x() - xOffset;
        int steps = relX/xStep;
        int newSteps = newRelX/xStep;
        setWidth(getWidth()+xStep*(steps-newSteps));
        setInitialTime(QString::number(getTime().toInt()+(steps - newSteps) ) );// my func
    }
}
if(event->type()== 187)
{
    dynamic_cast<agnt::A_GntGV*>(scene()->views().at(0))->timeChanged(getT(),getId());
    //my func, has nothing to do with painting
}

if(event->type()==160||event->type()==162)
    return false;
return true;

EDIT 2: I think sceneEventFilter() doesnt even matter. The problem persist if I comment

//grabber->installSceneEventFilter(this);

Solution

  • A reason why paint() is not called is that the boundingRect() function of your symbol does not return a proper value.

    Try to redefine the boundingRect() function for your item. It must return a QRectF.