PDA

View Full Version : Why does the QTextCursor.insertImage exist when it's not possible to get the image



kea_
13th August 2010, 11:46
Hello forum,
I spendet a lot of hour just to add images to a QTextDocument (ok that was fast) but I can't get the picture out from this QTextDocument. Why in the hell is there a insertImage if it's not possible to get the picture. I have to save this QTextDocument and maybe edit it. Or is there another possibility for using RichText?

Greetings and thanks for your answer.
kea_

The Storm
13th August 2010, 22:40
Create a QTextCursor and position it somewhere where the image is(you can iterate over the document until you find an image). In order to know if your cursor is on the image call charFormat(), then on the returned QTextCharFormat call isImageFormat(). If isImageFormat() returns true then cast the QTextCharFormat object to QTextImageFormat, then call the name() method. Using the name you can iterate over some of your created caches for images(maybe QHash<QString, QPixmap>) and get the real image. :)

kea_
14th August 2010, 17:43
Thank you very much for your answer.
I will check it out the next days.

Greetings kea_

kea_
15th August 2010, 00:16
Hello Storm,
thank you very much.
Now it works in the way I like. :)

For all they like to do similar or as hint. The source.

void UiArticleEdit::save(QString fileName){
QTextFrame *rootFrame = document()->rootFrame();
if(rootFrame) processFrame(rootFrame);
}

void UiArticleEdit::processFrame(QTextFrame *textFrameFather){
qDebug() << "processFrame";

for(QTextFrame::iterator it = textFrameFather->begin(); !(it.atEnd()); ++it){
QTextFrame *textFrame = it.currentFrame();
QTextBlock textBlock = it.currentBlock();

if(textFrame){
processFrame(textFrame);
}else if(textBlock.isValid()){
processBlock(textBlock);
}
}
}


void UiArticleEdit::processBlock(QTextBlock &textBlockFather){
qDebug() << "processBlock";

for(QTextBlock::iterator it = textBlockFather.begin(); !(it.atEnd()); ++it){
QTextFragment textFragment = it.fragment();
if(textFragment.isValid()){
QTextCharFormat textCharFormat = textFragment.charFormat();
QTextImageFormat textImageFormat = textCharFormat.toImageFormat();
if(textImageFormat.isValid()){
QTextDocument *textDocument = document();
QVariant variantImage = textDocument->resource(QTextDocument::ImageResource, textImageFormat.name());
if(variantImage.isValid()){
QImage image(qvariant_cast<QImage>(variantImage));
image.save(textImageFormat.name());
qDebug() << textImageFormat.name() << " -> " <<image.size();
}

qDebug() << "image" << textImageFormat.name();
}
}
}
}


Greetings kea_

The Storm
15th August 2010, 12:23
Glad I helped. My hints were not very accurate as I see but I haven't doing this since a long time. Nice that you figured it out. :)