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. QTextDocument mix of headline picture and HTML formated text
Forum Updated to NodeBB v4.3 + New Features

QTextDocument mix of headline picture and HTML formated text

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 6.9k Views 1 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.
  • E Offline
    E Offline
    enforcer
    wrote on last edited by
    #1

    My program has following informations in a list:

    • QTextLine for headline
    • QTextEdit for some informations (will be translated into html code for some format)
    • QImage which was rendered in same application with drawing widget

    I want to put these informations into a QTextDocument and print it as PDF in the following order per page:

    Headline

    Image

    Information

    I would like to know how i can put them into this QTextDocument. Except for QTextDocument::setHtml(...) and ::setPlainText(...) there is no way to add images. Can you help me?

    lg Enforcer

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

      Hi,

      You might be interested by the "Rich Text Processing":http://qt-project.org/doc/qt-4.8/richtext.html documentation

      Hope it helps

      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
      0
      • E Offline
        E Offline
        enforcer
        wrote on last edited by
        #3

        Thanks, now i can have Headline, Image and Information in my PDF, but now i have another problem.

        I have a list of many items which consists of headline, image and information, and I want to have each item on a single page each (item[0] at first page, item[1] at second page etc.). How can I add a new page?

        Please show me an example code, I read many forums which discuss the QTextDocument and new page problem, and neither of them explains how to manually make a new page.

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

          IIRC you have to add a QTextBlockFormat and set it's page break policy to QTextFormat::PageBreak_AlwaysAfter

          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
          0
          • E Offline
            E Offline
            enforcer
            wrote on last edited by
            #5

            This is what I got so far for my "printPdf" function:

            • create QPrinter and set the necessary settings like output format and file name
            • create QTextDocument instance
            • create QTextCursor and use instance of QTextDocument
            • for each item in the list:
              ** cursor.insertHtml(<item's headline>);
              ** cursor.insertImage(<item's image>);
              ** cursor.insertHtml(<item's info>);
              ** document.print(&printer);
              ** make new page (this part is not working)

            I want to use html because it's easier (for me) for text format. From an example I found this code:

            @QTextEdit myEdit;
            QTextDocument* myDocument = new QTextDocument(&myEdit);
            myEdit.setDocument(myDocument);
            QTextCursor* myCursor = new QTextCursor(myDocument);

            QTextBlockFormat format;
            format.setBackground(Qt::red);
            myCursor->setBlockFormat(format);

            myCursor->insertText("the ");

            format.setBackground(Qt::green);
            myCursor->insertBlock(format);
            myCursor->insertText("fish ");

            format.setBackground(Qt::yellow);
            myCursor->insertBlock(format);
            myCursor->insertText("are ");

            format.setBackground(Qt::red);
            myCursor->insertBlock(format);
            myCursor->insertText("coming!");

            format.setBackground(Qt::green);
            myCursor->insertBlock(format);
            myCursor->insertText(QString("%1 blocks").arg(myDocument->blockCount()));
            myEdit.show();@

            I hope I get it in the right direction:

            @QTextBlockFormat format;
            format.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysAfter);

            foreach(Item item, itemlist)
            {
            cursor.insertBlock(format);
            cursor.insertHtml("<item's headline>");
            cursor.insertImage("<item's image>");
            cursor.insertHtml("<item's info>");
            }
            document.print(&printer);@

            Do you think this will work?

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

              AFAICT, it seems all right. The best is to run it.

              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
              0
              • E Offline
                E Offline
                enforcer
                wrote on last edited by
                #7

                It seems QTextDocument can't insert QImage properly. The resolution is bad. When calling QImage::save(QString) there is no problem with resolution, but the same image in PDF file created by QPrinter the resolution sucks. I also tried to set QPrinter(QPrinter::HighResolution), but no changes in image quality.

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

                  Did you try to include the image through html ?

                  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
                  0
                  • E Offline
                    E Offline
                    enforcer
                    wrote on last edited by
                    #9

                    No, through QTextCursor::insertImage(QImage&) because I draw the image within the application via QPainter. But I already solved the problem to rise its resolution:

                    "QTextCursor source code":https://code.google.com/p/phantomjs/source/browse/src/qt/src/gui/text/qtextcursor.cpp?r=08fc50d149745b078c9fd73a213c8c83307beaa2

                    If you look at line 2311, then you see that the code transforms QImage into QTextImage format which would occure to this pixelized image in PDF. Afterwards it calls insertImage(QTextImageFormat&)

                    Instead of inserting a QImage I transformed it into an QTextImageFormat myself:

                    @QImage img(item->getImage(1920,1080));
                    // I wrote class Item

                    document.addResource(QTextDocument::ImageResource,
                    QUrl(item->getName()), img);

                    // Create QTextImageFormat and change its width, height alters according to image's width and height rate

                    QTextImageFormat imgFormat;
                    imgFormat.setName(item->getName());
                    imgFormat.setWidth(480);
                    cursor.insertImage(imgFormat);@

                    The Image in pdf has a high resolution (of course pdf has a bigger size as well).

                    QTextImageFormat keeps its width and height according to the loaded image (either QImage or from a PNG/JPEG), but the resolution drops. with QTextImage::setWidth(int) you can change width and height until it has the width you want. Resolution looks better because each pixel is compressed.

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

                      Nice catch !

                      I wonder if this behavior should not be changed to use the size of the image when creating the QTextImageFormat...

                      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
                      0
                      • E Offline
                        E Offline
                        enforcer
                        wrote on last edited by
                        #11

                        When you scale QImage BEFORE you make a QTextImageFormat from it then you would have the same pixelized problem as before. When you use MS Word you also compress the inserted picture make the size smaller.

                        Last part of this "Export to PDF" is to make a text parser to transform item's information into html code with bold and italics. I thought of something like the parser of this text editor where words within asterisks become bold and words within underlines become italic. I also want to add (un)ordered lists.

                        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