Rich text to image in QT

自古美人都是妖i 提交于 2019-12-10 18:02:23

问题


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 ?


回答1:


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.




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/24284025/rich-text-to-image-in-qt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!