Print/save QTextDocument to Image
-
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ł -
Use QTextDocument::render() and render it into a QImage.
-
Maybe you have some examples how to use it??
-
Hi,
QImage image; QPainter painter(&image); myDocument.drawContents(&painter); image.save("myDocumentImage.png");
[edit: Fixed code SGaist]
-
Hi, there is no draw function in QTextDocument class :(
-
@apaczenko1993 its's most definitely there:
https://doc.qt.io/qt-6/qabstracttextdocumentlayout.html#drawbut it seems to require a PaintContext object, as. ti doesn't have a default argument
-
@J-Hilk , @SGaist
You refer toQAbstractTextDocumentLayout
. However, the OP is usingQTextDocument
, that does not inherit fromQAbstractTextDocumentLayout
and has no such method. Doesn't he need to go via QAbstractTextDocumentLayout *QTextDocument::documentLayout() const? -
@JonB Wrong method, I was thinking about QTextDocument::drawContents. Code updated.
-
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
-
Okey, after setting pageSize and scaling in Qpainter the document looks very well :) thanks for help