QTextDocument mix of headline picture and HTML formated text
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:
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?
You might be interested by the "Rich Text Processing":http://qt-project.org/doc/qt-4.8/richtext.html documentation
Hope it helps
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 at first page, item 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.
IIRC you have to add a QTextBlockFormat and set it's page break policy to QTextFormat::PageBreak_AlwaysAfter
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>);
** 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:
QTextDocument* myDocument = new QTextDocument(&myEdit);
QTextCursor* myCursor = new QTextCursor(myDocument);
I hope I get it in the right direction:
foreach(Item item, itemlist)
Do you think this will work?
AFAICT, it seems all right. The best is to run it.
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.
Did you try to include the image through html ?
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:
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:
// I wrote class Item
// Create QTextImageFormat and change its width, height alters according to image's width and height rate
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.
Nice catch !
I wonder if this behavior should not be changed to use the size of the image when creating the QTextImageFormat...
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.