I inserted several images into QTextEdit with QTextCursor, now how should I save this entire thing ?
My first thought was to iterate through all positions of QTextCursor, but I didn’t find the proper interface to use.
Sample code in use:
QTextCursor cursor = ui->textEdit->textCursor();
cursor.insertImage(QImage ("/secure/Common/Pictures/icons/gimp.svg"));
ui->textEdit->setTextCursor(cursor);
QTextDocument *document = ui->textEdit->document();
QStringList images;
QTextBlock b = document->begin();
while (b.isValid()) {
for (QTextBlock::iterator i = b.begin(); !i.atEnd(); ++i) {
QTextCharFormat format = i.fragment().charFormat();
bool isImage = format.isImageFormat();
if (isImage)
{
images << format.toImageFormat().name();
qDebug() << document->resource(QTextDocument::ImageResource,
QUrl(format.toImageFormat().name())).toByteArray().size();
}
}
b = b.next();
}
Quick-and-dirty solution
Just call
document->toHtml()and parse theimgtags.Clean solution
Iterate over all the
QTextFragmentin the text:Warning: I didn’t test this, it might not even compile.