Rich text for image in QT

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

If it were plain text, I could use the DrawText associated with the QPainter class . But rich text cannot be processed in the same way as we do not know that formatting is done.

Any suggestions on how to convert?

+4
source share
3 answers

You can use QTextEdit::document+ QTextDocument::drawContents. You do not need hacks with rendering widgets, as suggested by other authors, because there may be some problems with anti-aliasing settings.

+4
source

You can capture the contents of your QTextEdit as follows:

QTextEdit te("This is a rich text");
te.resize(100, 100);
QPixmap pix = QPixmap::grabWidget (&te, te.rect());
pix.save("test.png");
+5
source

, , widget render pixmap:

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

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

, QPixmap::grabWidget() .

+2

Source: https://habr.com/ru/post/1545059/


All Articles