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->setContextMenuPolicy(Qt::PreventContextMenu);
    view->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
    view->settings()->setAttribute(QWebSettings::XSSAuditingEnabled,0);
    view->settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,1);
    view->load(QUrl("qrc:///html/index.html"));
    view->showFullScreen();
    return app.exec();
    }@

    My resource file:

    @<RCC>
    <qresource prefix="/">
    <file>html/index.html</file>
    <file>html/style.css</file>
    <file>html/script.js</file>
    </qresource>
    </RCC>@

    My index.html file:

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

    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":http://qt-project.org/doc/qt-5/qtwebkitexamples-webkitwidgets-imageanalyzer-example.html 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
    {
    Q_OBJECT
    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.