PDA

View Full Version : Accesing Images in a html doc stored as QResource file



sRB
23rd October 2014, 06:02
Hi all,
I have a .qrc file in which i have stored a html file and few images in the same folder. The html file uses these images to display them. I'am setting this html document in a QTextbrowser. I am loading the html file using the sethtml() function. But i'm unable to load the images in the document. How do i get the images to be displayed? Here is my code:

QFile htmlString(":/pathtohtml");
QImage preview(":/img.png");

if(htmlString.open( QIODevice::ReadOnly))
{
textbrowser->setHtml(htmlString.readAll());
}

ChrisW67
23rd October 2014, 09:35
Write the HTML to use relative paths:


<html>
<body>
<img src="test.png"/> <!-- relative path >
</body>
</html>

put the HTML and image in the same place in the resources:


<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>./test.html</file>
<file>./test.png</file>
</qresource>
</RCC>

then load the HTML using QTextBrowser::setSource():


#include <QApplication>
#include <QTextBrowser>

int main(int argc, char **argv)
{
QApplication app(argc, argv);
QTextBrowser tb;
tb.setSource(QUrl(QLatin1String("qrc:/test.html")));
tb.show();
return app.exec();
}

sRB
23rd October 2014, 11:29
Hi,
Thanks alot .
I'm using this html file as the first item to be loaded in a Qlist . The second item in the list is setting another html document which is created dynamically during runtime (without being stored as Qt resources data) using the sethtml() function. The hyperlinks within the second document don't seem to be working, when I click on any of the links there it brings me back to the first html document. I don't understand why this should happen. How do i clear the setsource() of the first document to make the textbrowser consider the current one?

ChrisW67
23rd October 2014, 20:51
I am not sure exactly what your problem is.

You load the new content into the QTextBrowser by calling setSource() or setHtml() with the new content. The old content is replaced.

If the generated HTML must link to resources that are in the resource file then you should generate URIs using the qrc: scheme to identify those resources. So, calling setHtml() with this text should also work:


<html>
<body>
<img src="qrc:/test.png"/> <!-- URI uses qrc: scheme -->
</body>
</html>


You cannot trivially hard code a link into the QRC based file that will cause the loading of a arbitrary bunch of HTML from a variable.

sRB
24th October 2014, 07:40
Hi,
My Problem is that, I have a List that has two elements,
1) The first item in the list is, I have a html file which is stored as resource file and I'm accessing that file using textbrowser->setSource(QUrl(QLatin1String("qrc:/resources/test.html")));
2) The second item is, In my application I write code which creates html tags as strings and finally a large single string containing the whole document is passed as an argument to sethtml(resultString).
Both are displayed properly within the TextBrowser

Now, When I click on any of the hyperlinks within the html document created using setHtml() , the links don't seem to work.
So I was wondering why this behaviour should happen?

ChrisW67
24th October 2014, 20:33
Links to what? What do the URLs look like? What does "don't seem to work" actually mean?

sRB
30th October 2014, 04:56
Links are the hyperlinks in a html document. They refer to other sections within the same document. These are created using anchor tags <a>