Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Export as PDF UI Scene
Forum Updated to NodeBB v4.3 + New Features

Export as PDF UI Scene

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
8 Posts 3 Posters 1.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tacdin
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • T tacdin

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @tacdin See https://forum.qt.io/topic/111913/print-widget-to-pdf-with-the-minimal-content
      So, create a printer, create a painter with printer as parameter and call widget->render(&painter) on the widget you want to print.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      0
      • jsulmJ jsulm

        @tacdin See https://forum.qt.io/topic/111913/print-widget-to-pdf-with-the-minimal-content
        So, create a printer, create a painter with printer as parameter and call widget->render(&painter) on the widget you want to print.

        T Offline
        T Offline
        tacdin
        wrote on last edited by
        #3

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

        jsulmJ 1 Reply Last reply
        0
        • T tacdin

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          T 1 Reply Last reply
          0
          • jsulmJ jsulm

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

            T Offline
            T Offline
            tacdin
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              KDAB has a very interesting article on the matter.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              T 2 Replies Last reply
              1
              • SGaistS SGaist

                Hi,

                KDAB has a very interesting article on the matter.

                T Offline
                T Offline
                tacdin
                wrote on last edited by
                #7

                @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
                {
                    Q_OBJECT
                
                public:
                    PdfExporter(QQmlApplicationEngine * engine);
                    Q_INVOKABLE void screenShot();
                  //  Q_INVOKABLE void writePdf();
                private:
                    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);
                        pdfPrinter.setOutputFormat(QPrinter::PdfFormat);
                        pdfPrinter.setPaperSize(QPrinter::A4);
                        pdfPrinter.setOutputFileName("test.pdf");
                        pdfPrinter.setResolution(600);
                        QPainter painter;
                        painter.begin(&pdfPrinter);
                
                        painter.drawImage(QRect(0,0,windowImage.width(),windowImage.height()),windowImage,{0,0,windowImage.width(),windowImage.height()});
                        painter.end();
                      }
                    }
                }
                

                for .qml file

                Button
                        {
                            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?

                1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  KDAB has a very interesting article on the matter.

                  T Offline
                  T Offline
                  tacdin
                  wrote on last edited by
                  #8

                  @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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved