J V
J V

Reputation: 515

Rich text to image in QT

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 ?

Upvotes: 4

Views: 2097

Answers (3)

Dmitry Sazonov
Dmitry Sazonov

Reputation: 9014

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.

Upvotes: 5

Andreas Fester
Andreas Fester

Reputation: 36649

Alternatively, as also stated in the comments, you can use the widget's render method to draw the widget contents into a pixmap:

void saveImage(QTextEdit* te) {
    QPixmap pixmap(te->size());
    QPainter painter(&pixmap);

    te->render(&painter);
    pixmap.save("test.png");
}

This is essentially what the QPixmap::grabWidget() method does internally.

Upvotes: 2

vahancho
vahancho

Reputation: 21258

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");

Upvotes: 5

Related Questions