Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rich text to image in QT

Tags:

qt

qpainter

In my application , have a QTextEdit dialog that accepts Rich Text Input. I need to convert this input in to an image for some purpose .

If it was Plain text i could use DrawText associated with the QPainter class . But Rich text cannot be dealt the same way as we don't know the formatting done.

Any suggestions on how to convert ?

like image 214
J V Avatar asked Dec 19 '22 13:12

J V


2 Answers

You can grab the content of your QTextEdit in the following way:

QTextEdit te("This is a rich text");
te.resize(100, 100);
QPixmap pix = QPixmap::grabWidget (&te, te.rect());
pix.save("test.png");
like image 199
vahancho Avatar answered Jan 16 '23 02:01

vahancho


You may use QTextEdit::document + QTextDocument::drawContents. You don't need any hacks with rendering widgets, as proposed by other authors, because there may be some problems with anti-aliasing settings.

like image 20
Dmitry Sazonov Avatar answered Jan 16 '23 02:01

Dmitry Sazonov