Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Print/save QTextDocument to Image
Forum Updated to NodeBB v4.3 + New Features

Print/save QTextDocument to Image

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 6 Posters 846 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.
  • A Offline
    A Offline
    apaczenko1993
    wrote on last edited by
    #1

    Hello all
    Fast question, does anybody know how save QTextDcument as a PNG file? I know that I can print QTextDcument as a pdf file but i need a PNG Image file. BR Michał

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Use QTextDocument::render() and render it into a QImage.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • A Offline
        A Offline
        apaczenko1993
        wrote on last edited by
        #3

        Maybe you have some examples how to use it??

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

          Hi,

          QImage image;
          QPainter painter(&image);
          myDocument.drawContents(&painter);
          image.save("myDocumentImage.png");
          

          [edit: Fixed code SGaist]

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

          1 Reply Last reply
          2
          • A Offline
            A Offline
            apaczenko1993
            wrote on last edited by
            #5

            Hi, there is no draw function in QTextDocument class :(
            2022-08-22_07h09_39.jpg

            J.HilkJ 1 Reply Last reply
            0
            • A apaczenko1993

              Hi, there is no draw function in QTextDocument class :(
              2022-08-22_07h09_39.jpg

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @apaczenko1993 its's most definitely there:
              https://doc.qt.io/qt-6/qabstracttextdocumentlayout.html#draw

              but it seems to require a PaintContext object, as. ti doesn't have a default argument


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              JonBJ 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @apaczenko1993 its's most definitely there:
                https://doc.qt.io/qt-6/qabstracttextdocumentlayout.html#draw

                but it seems to require a PaintContext object, as. ti doesn't have a default argument

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @J-Hilk , @SGaist
                You refer to QAbstractTextDocumentLayout. However, the OP is using QTextDocument, that does not inherit from QAbstractTextDocumentLayout and has no such method. Doesn't he need to go via QAbstractTextDocumentLayout *QTextDocument::documentLayout() const?

                SGaistS J.HilkJ 2 Replies Last reply
                0
                • JonBJ JonB

                  @J-Hilk , @SGaist
                  You refer to QAbstractTextDocumentLayout. However, the OP is using QTextDocument, that does not inherit from QAbstractTextDocumentLayout and has no such method. Doesn't he need to go via QAbstractTextDocumentLayout *QTextDocument::documentLayout() const?

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @JonB Wrong method, I was thinking about QTextDocument::drawContents. Code updated.

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

                  1 Reply Last reply
                  2
                  • JonBJ JonB

                    @J-Hilk , @SGaist
                    You refer to QAbstractTextDocumentLayout. However, the OP is using QTextDocument, that does not inherit from QAbstractTextDocumentLayout and has no such method. Doesn't he need to go via QAbstractTextDocumentLayout *QTextDocument::documentLayout() const?

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @JonB I actually got to that documentation entry via QTextDocument, I swear, but I can't reproduce it now 🤷‍♂️

                    I only end up with drawContext, @SGaist mentioned


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    JonBJ 1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @JonB I actually got to that documentation entry via QTextDocument, I swear, but I can't reproduce it now 🤷‍♂️

                      I only end up with drawContext, @SGaist mentioned

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @J-Hilk said in Print/save QTextDocument to Image:

                      I swear, but I can't reproduce it now 🤷‍♂️

                      An hour has gone by now ;-)

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        apaczenko1993
                        wrote on last edited by
                        #11

                        Ok, now i can print document into png file but the resolution is very low

                        QImage image (doc->size().width(), doc->size().height(), QImage::Format_ARGB32_Premultiplied);
                        
                            image.fill(Qt::transparent);
                            QPainter painter(&image);
                            doc->drawContents(&painter);
                            painter.end();
                            image.save("Test.png");
                        

                        How can I set higher resolution of this picture? At this moment the resolution of document is 997x566

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          ChrisW67
                          wrote on last edited by
                          #12

                          What page size have you set on the QTextDocument?

                          1 Reply Last reply
                          2
                          • A Offline
                            A Offline
                            apaczenko1993
                            wrote on last edited by apaczenko1993
                            #13

                            Okey, after setting pageSize and scaling in Qpainter the document looks very well :) thanks for help

                            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