QPainter::setClipRect(...) is not working with QSvgGenerator
How can we clip a painter linked to a QSvgGenerator paint device? I have the following example:
QSvgGenerator generator; generator.setFileName("test_svg_file.svg"); generator.setSize(size()); generator.setViewBox(rect()); QPainter painter; painter.begin(&generator); painter.setRenderHints(QPainter::Antialiasing); painter.setClipRect(QRect(100, 100, width() - 101, height() - 101)); painter.drawLine(0, 0, width() - 1, height() - 1); painter.end();
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.
What is the problem?
Are you absolutely sure the clipping rect is valid?
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.
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: https://wiki.python.org/moin/PyQt/Clipping 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 (https://bugreports.qt.io/browse/QTBUG-28636). Looks like there will not be a bug fix.
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 ?
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.