Important: Please read the Qt Code of Conduct -

Mixing qrc and file schemes in a webkit app

  • Hi guys!

    What I want to accomplish is quite simple; but first of all here is the source for my project so you have an idea.

    My main cpp file:

    @#include <QApplication>
    #include <QWebView>
    int main(int argc, char **argv)
    QApplication app(argc, argv);
    QWebView *view = new QWebView();
    view->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
    return app.exec();

    My resource file:

    <qresource prefix="/">

    My index.html file:

    @<!DOCTYPE html>
    <html lang="en">
    <meta charset="UTF-8">
    <title>Hello World!</title>
    <link type="text/css" rel="stylesheet" href="style.css" />
    <p><h1>Hello World!</h1></p>
    <video width="320" height="240" controls>
    <source src="myvideo.mp4" type="video/mp4">

    What I want is to be able to embed index.html, style.css and script.js files in my resource file and load myvideo.mp4 file from local file. Relative path here will assume I'm still using qrc scheme and I can not use absolute path with file scheme because I don't know where my app's path is. I know I can use "webkit's bridge": but how to achieve that? Is there any other way to do that to?

    Thank you.

  • I decided to use a webkit bridge through a simple class:
    class MyPath : public QObject
    public slots:
    Q_INVOKABLE QString get() {
    return QUrl::fromUserInput(QApplication::applicationDirPath()).toString();

    That'll be passing the absolute path of the app to my script:

    view->page()->mainFrame()->addToJavaScriptWindowObject(QString("myPath"), new MyPath);

    And from there I'll load my video with my script:

    document.querySelector("#myVideo > source").src = myPath.get()+"/myvideo.mp4";

    I'll let this thread as unsolved since I've got no answer and maybe there is a better approach. :-)

Log in to reply