<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QPainter ignore the text when the clipping is applied while printing to a PDF document]]></title><description><![CDATA[<p dir="auto">In a project, I'm trying to print the content of a view in a PDF file. As the document to print contains several advanced graphic items, I needed to implement my own printing function, which should manage the pages cutting.</p>
<p dir="auto">To implement the pages cutting, I wrote a function which will detect when the drawing exceeds a given page, and will continue the draw on the next page by translating and repeating the current item drawing, after applying a clipping rectangle on the page. This works well for the graphic items, but doesn't work for the text, as you can see on the following screenshot.</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/ffe354de-f726-4956-8061-6e6ee8c5df50.png" alt="Text clipping issue.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">My clipping rectangle is defined on the main QPainter I use to draw the page, in the following manner:</p>
<pre><code>// clip the margins surrounding the page
painter.setClipRect(25, 0, 2100, 2970);
painter.setClipping(true);
</code></pre>
<p dir="auto">Then I link this painter to the printer and I use it to draw both the graphic items and the texts. The texts are painted from a QTextDocument, like that:</p>
<pre><code>// draw the text
textDoc.drawContents(&amp;painter, textRect);
</code></pre>
<p dir="auto">I know that the drawContents() function clips the rect around the textRect passed in parameters. But why my global clipping is ignored here? Is there a way to include the text in the global clipping? And if yes, how to do that?</p>
]]></description><link>https://forum.qt.io/topic/113665/qpainter-ignore-the-text-when-the-clipping-is-applied-while-printing-to-a-pdf-document</link><generator>RSS for Node</generator><lastBuildDate>Thu, 23 Apr 2026 02:26:43 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/113665.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 14 Apr 2020 14:48:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QPainter ignore the text when the clipping is applied while printing to a PDF document on Wed, 15 Apr 2020 10:17:14 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
It seems to set its own clipping on the painter. overwriting yours.</p>
<pre><code>void QTextDocument::drawContents(QPainter *p, const QRectF &amp;rect)
{
    p-&gt;save();
    QAbstractTextDocumentLayout::PaintContext ctx;
    if (rect.isValid()) {
        p-&gt;setClipRect(rect);
        ctx.clip = rect;
    }
    documentLayout()-&gt;draw(p, ctx);
    p-&gt;restore();
}
</code></pre>
]]></description><link>https://forum.qt.io/post/588780</link><guid isPermaLink="true">https://forum.qt.io/post/588780</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Wed, 15 Apr 2020 10:17:14 GMT</pubDate></item><item><title><![CDATA[Reply to QPainter ignore the text when the clipping is applied while printing to a PDF document on Thu, 16 Apr 2020 20:26:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> Thank you for your answer. So I tested to remove the text rectangle passed to the drawContents() function, and indeed the global clipping was applied instead.</p>
]]></description><link>https://forum.qt.io/post/589179</link><guid isPermaLink="true">https://forum.qt.io/post/589179</guid><dc:creator><![CDATA[jeanmilost]]></dc:creator><pubDate>Thu, 16 Apr 2020 20:26:43 GMT</pubDate></item><item><title><![CDATA[Reply to QPainter ignore the text when the clipping is applied while printing to a PDF document on Wed, 15 Apr 2020 10:17:14 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
It seems to set its own clipping on the painter. overwriting yours.</p>
<pre><code>void QTextDocument::drawContents(QPainter *p, const QRectF &amp;rect)
{
    p-&gt;save();
    QAbstractTextDocumentLayout::PaintContext ctx;
    if (rect.isValid()) {
        p-&gt;setClipRect(rect);
        ctx.clip = rect;
    }
    documentLayout()-&gt;draw(p, ctx);
    p-&gt;restore();
}
</code></pre>
]]></description><link>https://forum.qt.io/post/588780</link><guid isPermaLink="true">https://forum.qt.io/post/588780</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Wed, 15 Apr 2020 10:17:14 GMT</pubDate></item></channel></rss>