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 858 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.
  • 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 Online
        J.HilkJ Online
        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 Online
              J.HilkJ Online
              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