PDA

View Full Version : How to show remote images in QTextEdit from html-string?



okta
28th June 2012, 23:40
Hello guys! Need some help!
How to show remote images in QTextEdit from html-string?

d_stranz
29th June 2012, 00:43
QTextEdit supports the HTML 4.0 <a href=... /> element, so presumably you could put the URL of your image in that. See this link (http://qt-project.org/doc/qt-4.8/richtext-html-subset.html).

okta
29th June 2012, 08:00
But it also supports "img Image Supports the src, source (for Qt 3 compatibility), width, and height attributes." You think changing to hrf will help?

Added after 15 minutes:

I'm trying to do it with QTextEdit::loadResource, but something goes wrong

mTextEdit->setHtml(text);
page = new QWebPage();
QWebFrame* frame;
frame = page->mainFrame();
frame->setHtml(text);

foreach (QWebElement element, frame->findAllElements("img")) {
QStringList attributesList = element.attributeNames();
foreach (QString AttributeName, attributesList) {
if(AttributeName == "src"){
QUrl url;
url = element.attribute(AttributeName);
mTextEdit->loadResource(QTextDocument::ImageResource, url);
qDebug() <<AttributeName <<":" << element.attribute(AttributeName);
}
}
}

I can find every image link, but after loadResource nothing happen(

d_stranz
29th June 2012, 16:13
I think loadResource() is intended for loading things from the resources compiled into the code from the resource (qrc) file, not for loading remote files from the disk or the internet.

The "src" attribute is supported in HTML 4, you should check the WC3 HTML 4.01 specification (http://www.w3.org/TR/REC-html40/). Click on the link to "attributes" at the top of the page to see the table of allowed attributes and the format of their values.

It is possible that QTextEdit simply doesn't support loading remote images. I don't know, I have not tried this.