Important: Please read the Qt Code of Conduct -

QPainter::setClipRect(...) is not working with QSvgGenerator

  • Hi,

    How can we clip a painter linked to a QSvgGenerator paint device? I have the following example:

            QSvgGenerator generator;
    	QPainter painter;
    	painter.setClipRect(QRect(100, 100, width() - 101, height() - 101));
    	painter.drawLine(0, 0, width() - 1, height() - 1);

    in which the painter is supposed to clip the line, but it is not. The output SVG file has line from "top-left" to "bottom-right".

    Comment: In a child widget, I'm drawing a painter path (QPainterPath) going up and down. Since clipping is ignored by QSvgGenerator (I'm assuming), the SVG file shows path outside the child widget too.

    What is the problem? How can I clip all painter drawings?

    Thanks in advance.

  • Moderators

    @soma_yarram said in QPainter::setClipRect(...) is not working with QSvgGenerator:

    What is the problem?

    Are you absolutely sure the clipping rect is valid?
    What does:

    qDebug() << QRect(100, 100, width() - 101, height() - 101);

    print to the terminal/application output view?

  • Hi.....the entire code snippet is in a widget's slot. In my case, widget's width and height are above 500px, which means the clip rect is valid. Moreover the code snippet is just an example where I want to clip a line drawn from "top-left" to "bottom-right" leaving 100px border in all sides.

    The output SVG file has a line from "top-left" to "bottom-right" which is not clipped. But when I change paint device to QImage or QPixmap, it works as expected, the line is clipped. The problem comes with QSvgGenerator.

  • Moderators

    It may be a bug, have you checked the bugtracker to see if something's known?

  • I could not find if it is a bug or the feature is not at all implemented, but I found a reference in the page: SVG output, where it says that QSvgGenerator doesn't support clipping.

    If it is so, then I may have to solve clipping issue in another way.

    Do you have any ideas? or is there any other vector image format other than SVG supported by Qt?

  • Yes....I found bugs related to SVG clipping already reported ( Looks like there will not be a bug fix.

  • Lifetime Qt Champion


    The bug linked in the comment suggests to apply the clipping before rendering to the SVG in order to avoid creating files with data that would ultimately be unused. Would that be an acceptable workaround ?

  • Hi,

    In my case, I'm simply drawing a QPainterPath, which is easy to clip with a specified QRect (border).

    But I have a problem with this approach: I'm using QPainterPath::intersected(const QPainterPath &p) const method which returns clipped path, which is a single connected unit. I want to have multiple disconnected units:


    How can I get such multiple disconnected units? Thanks in advance.

Log in to reply