Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Clipping is not working !



  • Hi,

    I have made a QGraphicsItem called Cursor. I have reimplemented the paint method and after drawing the item I have added painter->setClipRect(QRectF(0, 0, 100, 100)); The size of the rectangle is just for testing.
    I have also added the same rectangle to the scene to get visual references.
    Unfortunately the cursor remains visible even if I move outside the rectangle.
    391ea3a7-0040-45ea-a428-24f6df46e8cf-image.png
    What could be the reason for this behavior? What did I wrong?


  • Moderators

    You are calling setClipRect after everything is already painted. This method does not work like an eraser that will clear everything outside that rectangle. It only sets a clip area for all following painting methods i.e. anything that uses that painter afterwards will not draw outside of that region.
    So for it to do anything useful in this case you must call it before you start drawing any lines.


  • Moderators

    It's hard to say without more context but I'm guessing you're calling this inside item's paint() method? The painter coordinates in that method are local to the item so if you set the clip rect like that it's gonna move along with the item. If you want to clip it relatively to the view you'll gonna have to translate that rect from item coordinates to view coordinates, i.e. adjust for item's position in the scene and scene's position in the view.



  • Thank you for your answer.
    Yes I'm calling clipRect from inside item's paint() method.
    I'm still struggling with this issue.
    If the size of the item is 50x50 and I set painter->setClipRect(QRectF(0, 0, 20, 20)) should I not only see a part of the item?

    Here is the code of the paint method

    void Cursor::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        qreal innerSize = m_size * 0.2;
        if (innerSize>10)
            innerSize = 10;
        else if (innerSize<5)
            innerSize=5;
    
        qreal halfInnerSize = innerSize / 2;
        QPen pen;
        pen.setColor(m_color);
        painter->setPen(pen);
        painter->drawRect(QRectF(-halfInnerSize, -halfInnerSize, innerSize, innerSize));
    
        painter->drawLine(QLineF(0, -halfInnerSize, 0, halfInnerSize));
        painter->drawLine(QLineF(-halfInnerSize, 0, halfInnerSize, 0));
        pen.setWidth(penWidth);
        painter->setPen(pen);
        qreal halfSize = m_size/2;
        qreal gap = 0.15*m_size;
        painter->drawLine(QLineF(0, -halfSize, 0, -gap));
        painter->drawLine(QLineF(0, gap, 0, halfSize));
        painter->drawLine(QLineF(-halfSize, 0, -gap, 0));
        painter->drawLine(QLineF(gap, 0, halfSize, 0));
    
        QRectF rect;
        rect.setRect(0, 0, 50, 50);
        QPointF point(mapToScene(0, 0));
        qDebug() << "Point" << point;
        qDebug() << "Scene pos: " << this->scenePos();
        painter->setClipRect(0, 0, 10, 10);
    
    }
    

  • Moderators

    You are calling setClipRect after everything is already painted. This method does not work like an eraser that will clear everything outside that rectangle. It only sets a clip area for all following painting methods i.e. anything that uses that painter afterwards will not draw outside of that region.
    So for it to do anything useful in this case you must call it before you start drawing any lines.



  • Thank you for your help.
    If you know how it works under the hood it’s obvious that setClipRect has to be placed first.
    Sometimes the Qt help should be a bit more accurate, especially if you are a beginner.


  • Moderators

    No worries, I'm glad I could help.
    Clipping works like that in pretty much all major drawing APIs. It's the same in GDI, OpenGL and others, not just in Qt.
    I guess nobody just thought that it could be understood differently having some experience with any of them, but if you're a beginner I can see how this could happen.


Log in to reply