Important: Please read the Qt Code of Conduct -

Export as PDF UI Scene

  • Hi,

    I want the images and texts appearing in the interface to be rendered as PDF. I have a button and when the button is triggered, it saves whatever is on the screen that appears in the interface as a PDF(like export as PDF) . For this, I looked at the resources and the topics opened in the forum, but I could not reach a clear result. I would be very happy if you could help with this.

  • Lifetime Qt Champion

    @tacdin See
    So, create a printer, create a painter with printer as parameter and call widget->render(&painter) on the widget you want to print.

  • @jsulm Thank you so how can I bind this to QML.

  • Lifetime Qt Champion

    @tacdin That I don't know (I'm not a QML expert, didn't notice you posted in QML forum).

  • @jsulm oh, that's okay ,maybe you know a LifeTime QT Champion for QML and this topic, you can tag it here. :p

  • Lifetime Qt Champion

  • @SGaist I'm sorry for the late reply. I just saw what you wrote. I tried something like this.

    hedader file

    #pragma once
    #include <QQmlApplicationEngine>
    class PdfExporter : public QQmlApplicationEngine
        PdfExporter(QQmlApplicationEngine * engine);
        Q_INVOKABLE void screenShot();
      //  Q_INVOKABLE void writePdf();
        QQmlApplicationEngine * mEngine;

    .cpp file

    #include <Drawer.h>
    #include <QQuickWindow>
    #include <QPrinter>
    #include <QPainter>
    #include <QPdfWriter>
    PdfExporter::PdfExporter(QQmlApplicationEngine *engine) : QQmlApplicationEngine(engine), mEngine(engine)
    void PdfExporter::screenShot()
        foreach(QObject* obj, mEngine->rootObjects()) {
          QQuickWindow* window = qobject_cast<QQuickWindow*>(obj);
          if (window)
            QImage windowImage = window->grabWindow();
            QPrinter pdfPrinter(QPrinter::HighResolution);
            QPainter painter;

    for .qml file

                Layout.preferredWidth:  100
                Layout.preferredHeight:  20
                Layout.alignment: Qt.AlignHCenter
                text: "test"
                onClicked: drawer.screenShot()

    but the resolution is too bad. Do you have a suggestion for this?

  • @SGaist What is explained here is not clear enough. There is no information about header files, any of the structures to #include, and that's why I'm getting so many errors. I wish you could elaborate a little more on this link. I'm sure it's very easy for you. However, as a beginner, its very difficult.

Log in to reply