开发者

Is it possible to embed html with js into QwebKit

will I need to load java script objects into QwebKit when my application is loading

I can use setHtml with the html and the java script string formatted but its very hard to Ma开发者_Go百科intain.

My question is can I embed the html and java script as resource into Qwebkit ?

Also I don’t what that the html and the javascript be available to the application users


Yes, you can.

Simply add your JS/CSS to the resource file (look up the Qt docs if you don't know how) and then in your HTML simply use qrc:/file/path/or/alias.

For example, if I add JQuery to the resource file, aliased as /js/jquery, then to load it, I'd place this into my HTML file:

<script type="text/javascript" src="qrc:/js/jquery"></script>

Loading a HTML file from a resource file seems to be more effort as I can't seem to get QWebKit::setUrl(QUrl("qrc:/resource/path")) to work (":/resource/path" doesn't work for me either). What I currently do is this:

QString readFile (const QString& filename)
{
    QFile file(filename);
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream stream(&file);
        return stream.readAll();
    }
    return "";
}

...
myWebKitObject->setHtml(readFile("qrc:/html/index"));

Creating links to other HTML resources may be a problem, though. Should anyone know how to deal with this, please comment! (I imagine you can do this by reimplementing the network managerclass to intercept when webkit sends http requests and if they're for qrc:/ urls, read the file from the resource system and respond with that, though then you have to manage mime types and such (eg, if the resource is an image) yourself...)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜