July 4, 2011

Stavros Filippidis Stavros Filippidis
Lab Rat
388 posts

[SOLVED] Load an html file from a Qt Resource file

 

I have created a Qt Resource file with an html file. I want to load it and make it display in the standard web-browser. I use:

  1. QDesktopServices::openUrl(QUrl("qrc:/prefix/filename.html"));

but it doesn’t work!

Please notice that for normal URLs, (http://…) it works like a charm!

Any ideas?

8 replies

July 4, 2011

bergo.torino bergo.torino
Lab Rat
41 posts

Show your .qrc file, maybe you have wrong path (some typos).

 Signature 

It’s compiling - mostly I’m slacking off ;)

July 5, 2011

Volker Volker
Ant Farmer
5428 posts

This will never work.

QDesktopServices::openUrl() usually calls the operating system’s default application to open the URL. As qrc is not a standard scheme, there is no application that will handle it. Additionally, Firefox, Explorer or Safari would not even know how to get the data behind the qrc file :)

You can set a custom handler, but that would be a QObject subclass within your application.

July 5, 2011

peppe peppe
Ant Farmer
1028 posts

Not only the qrc scheme is (usually) not associated with any application, but remember that resources are compiled inside your executable. How is a 3rd party program supposed to access them?

A workaround could be copying the file from the resource to a temporary file/directory and then use QUrl::fromLocalFile + QDesktopServices::openUrl.

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

July 5, 2011

Stavros Filippidis Stavros Filippidis
Lab Rat
388 posts
bergo.torino wrote:
Show your .qrc file, maybe you have wrong path (some typos).

The resource file is automatic created from Qt Creator, so I find it difficult to have typos! ;-)

July 5, 2011

Stavros Filippidis Stavros Filippidis
Lab Rat
388 posts

Volker wrote:
This will never work.

QDesktopServices::openUrl() usually calls the operating system’s default application to open the URL. As qrc is not a standard scheme, there is no application that will handle it. Additionally, Firefox, Explorer or Safari would not even know how to get the data behind the qrc file :)

You can set a custom handler, but that would be a QObject subclass within your application.

I understand… Could I just display the resource to a QTextBrowser, or something like that?

July 5, 2011

Stavros Filippidis Stavros Filippidis
Lab Rat
388 posts

peppe wrote:
Not only the qrc scheme is (usually) not associated with any application, but remember that resources are compiled inside your executable. How is a 3rd party program supposed to access them?

A workaround could be copying the file from the resource to a temporary file/directory and then use QUrl::fromLocalFile + QDesktopServices::openUrl.

Could you kindly provide some sample code for the workaround you provide? :-)

July 5, 2011

ZapB ZapB
Ant Farmer
1427 posts

Why not display your resource html page in a QWebView that way you get the full power of WebKit to help you.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

July 8, 2011

Stavros Filippidis Stavros Filippidis
Lab Rat
388 posts

peppe wrote:
Not only the qrc scheme is (usually) not associated with any application, but remember that resources are compiled inside your executable. How is a 3rd party program supposed to access them?

A workaround could be copying the file from the resource to a temporary file/directory and then use QUrl::fromLocalFile + QDesktopServices::openUrl.

Finally I followed a similar workaround, and solved the issue! :-)

 
  ‹‹ Design approach to feeding model with data      QML absolute images path work on windows , while don’t work on mac , why ››

You must log in to post a reply. Not a member yet? Register here!