Question

I've problem with open local html file in QWebView widget. So, I do it like this:

QWebView *myWebView = new QWebView;    
myWebView->load(QUrl("qrc:/index.htm"));

Sure, I have added index.htm in resources. But in QWebView there is white screen. I tried to open local file with Firefox, it's allright.

What should I do to fix it?

PS: htm-page uses js and css files, but I've also added them to resources too. PSS: Also, I tried to do it such way:

QFile res(":/index.htm");
res.open(QIODevice::ReadOnly|QIODevice::Text);
myWebView->setHtml(res.readAll());

but it doesn't help.

Was it helpful?

Solution

Have you tried just loading the file to QByteArray to verify it loads correctly?

Edit:

Something like (untested, but you get the idea):

QFile file(":/index.htm");
if(file.open(QIODevice::ReadOnly)) {
    QByteArray dump = file.readAll();
    qDebug() << "contents: " << dump;
} else {
    qDebug() << "error: " << file.error();
}

The error() method returns QFile::FileError enum.

OTHER TIPS

I would first try this:

connect(myWebView, SIGNAL(loadFinished(bool)), this, SLOT(finishedLoad(bool)));
....
void finishedLoad(bool ok){
    qDebug() << ok;
}

Then you will know if the web view is encountering an error while loading. The error could be due to improperly formatted html, etc.

Another method would be to load the contents of the resource into a string, then:

myWebView->setHtml(stringFromResource);

Your problem is in using resource file. Here is some advice's:

  • Is your file really located on drive?
  • Have you saved your resource file before build? Try to save all and do clean-build.
  • First of all check (and if needed, show) your .qrc-file (it is xml-like text file), is it correct? Have you added prefix (<qresource prefix="/">) and so on
  • Try to use aliases like <file alias="cut-img.png">images/cut.png</file> and your file is then accessible as :/cut-img.png from the application.

Of course, don't forget to check documentation -- http://qt-project.org/doc/qt-4.8/resources.html

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top