<?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[PDFWriter : offset after element rotation]]></title><description><![CDATA[<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/fee54a00-3b2b-40ea-8966-a30bab3d32a8.jpeg" alt="Capture d’écran 2026-12-04 à 10.29.05.jpeg" class=" img-fluid img-markdown" /><br />
Hello, my application is a graphical editor where the user moves and rotates 2D elements in order to create a printable/cutable pattern that I export to PDF. When I only move elements it works fine, but as soon as I rotate an element, its PDF output has an offset. On the screen capture, I rotated the element with the big '27' and it has now an offset. My pdf export code is straightforward, but maybe I forgot something. QML Elements are dragged and rotated using a MouseArea with drag.target = their parent and a WheelHandler with "rotation" as property, then their position X and Y and rotation angle are stored using on..Changed to the DataModel. The PDF output code uses that same datamodel. On my previous version using Widgets I used my own code for element drag and rotation, so it was easier to obtain WYSIWYG, but with QML that handles all that by itself, I don't see where I can intervene to correct the offset due to rotation.<br />
Here's my export to PDF code :</p>
<pre><code>void Donnees::exportePDF() {
    QBuffer buffer;
    buffer.open(QIODevice::WriteOnly);
    QPdfWriter pdfWriter(&amp;buffer);
    pdfWriter.setPageSize(QPageSize(QPageSize::A4));
    QPainter painter(&amp;pdfWriter);
    painter.setWindow(0, 0, 210, 297);
    painter.setViewport(0, 0, 9727, 13757);
    QPen pen(Qt::red, 0.2);
    pen.setJoinStyle(Qt::SvgMiterJoin);
    pen.setCapStyle(Qt::SquareCap);
    painter.setPen(pen);
    QPen penPli = pen;
    penPli.setColor(Qt::black);
    penPli.setStyle(Qt::DashLine);
    QList&lt;qreal&gt; dashes;
    dashes &lt;&lt; 8 &lt;&lt; 16;
    penPli.setDashPattern(dashes);

    QFont pf("Arial");
    pf.setPointSizeF(0.4f);
    painter.setFont(pf);
    QFontMetrics fm = painter.fontMetrics();

    // tri des pièces par page
    std::vector&lt;int&gt; pp;
    int pMin = 1000, pMax = 0;
    for (int i = 0; i &lt; m_pieces-&gt;rowCount(); i++) {
        int np = pageCalcNum((int)m_pieces-&gt;getPosX(i), (int)m_pieces-&gt;getPosY(i));
        pp.push_back(np);
    }

    std::sort(pp.begin(), pp.end());
    auto it = std::unique(pp.begin(), pp.end());
    pp.erase(it, pp.end());

    int n = 0;
    for (const auto np : pp) {
        bool npok = false;
        for (int i = 0; i &lt; m_pieces-&gt;rowCount(); i++) {
            int x = (int)m_pieces-&gt;getPosX(i);
            int y = (int)m_pieces-&gt;getPosY(i);
            int npi = pageCalcNum(x, y);
            if (npi == np) {
                if (n  &gt; 0) {
                    if (!npok) {
                        pdfWriter.newPage();
                        npok = true;
                    }
                }

                QPolygonF p = contourToPolygon(m_pieces-&gt;getContour(i));
                QPointF c = centroid(p);
                painter.save();
                painter.translate(c.x() + (x % 210), c.y() + (y % 297));
                painter.rotate(m_pieces-&gt;getAngle(i));
                painter.translate(-c.x(), -c.y());
                painter.setPen(pen);
                painter.drawPolygon(p);

                QString sPlis = m_pieces-&gt;getPlis(i);
                QStringList plis1 = sPlis.split(";");
                QList&lt;QLine&gt; pli;
                for (const auto&amp; pl : std::as_const(plis1)) {
                    QStringList plis = pl.split(" ");
                    pli.push_back(QLine(
                        plis[0].toInt(), plis[1].toInt(),
                        plis[2].toInt(), plis[3].toInt()
                    ));
                }
                painter.setPen(penPli);
                painter.drawLines(pli);


                std::vector&lt;NTxt&gt; nT = m_pieces-&gt;getnT(i);
                for (const auto nTi : nT) {
                    QString txt = QString("%1").arg(nTi.num);
                    int dh = fm.horizontalAdvance(txt);
                    painter.save();
                    painter.translate(nTi.x, nTi.y);
                    painter.rotate(nTi.a - 180);
                    painter.drawText(- dh/2, -2, txt);
                    painter.restore();
                }
                painter.restore();
            }
        }
        n++;
    }

    painter.end();
    QByteArray pdfData = buffer.data();
    QDir::setCurrent(QDir::homePath().append("/Documents"));
    QFileDialog::saveFileContent(pdfData, "monProjet.pdf");
}

</code></pre>
]]></description><link>https://forum.qt.io/topic/165322/pdfwriter-offset-after-element-rotation</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Dec 2026 01:37:59 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/165322.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 04 Dec 2026 10:13:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to PDFWriter : offset after element rotation on Fri, 04 Dec 2026 14:10:44 GMT]]></title><description><![CDATA[<p dir="auto">Apparently, the gap lies into the QML code that uses property bindings. when an element is rotated, I tried to also change its position, but if I do so all elements become jammed, so I tried to use a bool to prevent X and Y OnChanged to fire during such updates, but without any effect. I also tried to add a C++ member to change at the same time X, Y and Rotation, but it still doesn't work. Maybe do I need to momentarily disable the property binding (thing that I'm not sure can be done).<br />
here's my QML Code</p>
<pre><code>                    MouseArea { // DRAG pour déplacer la vue
                        anchors.fill : parent
                        acceptedButtons: Qt.LeftButton
                        drag.target: vue2D
                        cursorShape: Qt.ArrowCursor
                    }

                    Repeater { // UNE PIECE COMPLETE
                        model: pieces
                        delegate : Shape { id : pieceRoot
                            containsMode: Shape.FillContains
                            DragHandler {cursorShape: Qt.ClosedHandCursor}
                            states: [
                                     State {
                                         name: ""
                                         PropertyChanges { pieceSP.strokeWidth: -1 }
                                     },
                                     State {
                                         name: "selected"
                                         PropertyChanges { pieceSP.strokeWidth: 2 }
                                     }
                                 ]
                            ShapePath { id: pieceSP // POLYGONE FORMANT LA PIECE
                                fillRule: ShapePath.WindingFill
                                fillColor: iToC[coul]
                                strokeColor:"yellow"
                                PathSvg { path: contour }
                            }

                            property real tmpPosX : posX
                            property real tmpPosY : posY
                            property real tmpAngle : angle
                            property bool rotEnCours : false

                            x: tmpPosX
                            y: tmpPosY
                            rotation: tmpAngle

                            onXChanged: {
                                if (!rotEnCours)
                                    pieces.setPosX(index, x);
                            }
                            onYChanged: {
                                if (!rotEnCours)
                                    pieces.setPosY(index, y);
                            }
                            onRotationChanged: {
                                rotEnCours = true;
                                pieces.setAngle(index, rotation);
                                rotEnCours = false;
                                // pieces.setPosX(index, x);
                                // pieces.setPosY(index, y);
                            }

                            Text { // NUMERO DE LA PIECE (aka n° de sa première facette)
                                font.pointSize: 10
                                color: "black"
                                text: num
                                horizontalAlignment: Qt.AlignHCenter
                                verticalAlignment: Qt.AlignVCenter
                                x: centre.x - (contentWidth /2)
                                y: centre.y - (contentHeight /2)
                            }
                            Repeater { // CHAQUE NUMERO D'ARETE (ENTRE 2 FACETTES)
                                model: ListModel {id: tliste}
                                Text {
                                    text: model.txt
                                    font.pointSize: 6
                                    horizontalAlignment: Qt.AlignHCenter
                                    verticalAlignment: Qt.AlignVCenter
                                    color: "#1500ff"
                                    transform: [
                                        Translate { x: 0; y: -7 },
                                        Rotation { angle: 180+model.a },
                                        Translate { x: model.x; y: model.y }
                                    ]
                                }
                            }
                            Component.onCompleted: creeNums(tliste, nTxt)

                            Repeater { // CHAQUE LIGNE DE CONTOUR
                                model: ListModel {id: cliste}
                                Shape {
                                    containsMode: Shape.FillContains
                                    HoverHandler { id: mouseCont
                                        cursorShape: Qt.CrossCursor
                                    }
                                    property int f1: model.f1
                                    property int f2: model.f2
                                    property var c: mouseCont.hovered ? "yellow" : "black"
                                    ShapePath {
                                        strokeWidth: 0.25
                                        strokeColor: c
                                        fillColor: c
                                        startX: model.x1
                                        startY: model.y1
                                        PathPolyline {
                                            path: [
                                                Qt.point(model.x2, model.y2),
                                                Qt.point(model.x3, model.y3),
                                                Qt.point(model.x4, model.y4),
                                                Qt.point(model.x1, model.y1),
                                            ]
                                        }
                                        pathHints: ShapePath.PathSolid || ShapePath.PathLinear || ShapePath.PathConvex;
                                    }
                                    TapHandler {
                                        onTapped: {
                                            faces.attache(num, f1, f2);
                                        }
                                    }
                                }
                                Component.onCompleted: creeLignes(lcont, cliste)
                            }

                            Repeater { // CHAQUE LIGNE DE PLI
                                model: ListModel {id: pliste}
                                Shape {
                                    containsMode: Shape.FillContains
                                    HoverHandler { id: mousePlis
                                        cursorShape: Qt.CrossCursor
                                    }
                                    property int f1: model.f1
                                    property int f2: model.f2
                                    property var c: mousePlis.hovered ? "red" : "white"
                                    ShapePath {
                                        strokeWidth: 0.25
                                        strokeColor: c
                                        fillColor: c
                                        startX: model.x1
                                        startY: model.y1
                                        PathPolyline {
                                            path: [
                                                Qt.point(model.x2, model.y2),
                                                Qt.point(model.x3, model.y3),
                                                Qt.point(model.x4, model.y4),
                                                Qt.point(model.x1, model.y1),
                                            ]
                                        }
                                        pathHints: ShapePath.PathSolid || ShapePath.PathLinear || ShapePath.PathConvex;
                                    }
                                    TapHandler {
                                        onTapped: {
                                            faces.detache(num, f1, f2);
                                        }
                                    }
                                }
                                Component.onCompleted: creeLignes(plis, pliste)
                            }

                            WheelHandler { // ALT + roulette pour tourner la pièce
                                acceptedModifiers: Qt.AltModifier
                                property: "rotation"
                                //rotationScale: 0.25
                            }

                            /*MouseArea { // clic gauche sur une pièce -&gt; selection / déselection
                                anchors.fill: pieceRoot
                                acceptedButtons: Qt.LeftButton
                                onClicked: {
                                    pieceRoot.state = pieceRoot.state === "" ? "selected" : "";
                                }
                            }*/
                        }
                    }
</code></pre>
]]></description><link>https://forum.qt.io/post/841143</link><guid isPermaLink="true">https://forum.qt.io/post/841143</guid><dc:creator><![CDATA[Gilboonet]]></dc:creator><pubDate>Fri, 04 Dec 2026 14:10:44 GMT</pubDate></item></channel></rss>