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?
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.
QTextEdit::document
QTextDocument::drawContents
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");
, , widget render pixmap:
render
void saveImage(QTextEdit* te) { QPixmap pixmap(te->size()); QPainter painter(&pixmap); te->render(&painter); pixmap.save("test.png"); }
, QPixmap::grabWidget() .
QPixmap::grabWidget()
Source: https://habr.com/ru/post/1545059/More articles:Problems with RTSP Live Streaming for Android - androidLint finds warnings in log4j used in Android project - androidпрерывание строки и абзац удваиваются каждый раз, когда я сохраняю документ ckeditor, добавляя лишние пробелы и разрывы строк после каждого сохранения - javascriptHow to enable ASLR, DEP and SafeSEH in exe in code blocks using mingw? - c ++The correct way to create and initialize objects in android - androidPowerShell script to export a list of file names only without a file extension, and then output to a text file separate for each directory - powershellHow to print a list of values from a Cs page to a presentation level (Aspx) - jsonunterminated string literal error for django template tag - javascriptFacebook login verification: submit a test version or production version? - iosHow to configure unit tests in Swift? - unit-testingAll Articles