Qt WebKit crossOrigin restriction when loading images.



  • Hello,

    I am building an application based on Qt WebKit, the application just loads index.html (located in a path relative to the executable). The html+js it self makes several XHR requests, and then tries to load images from cloud front (which by the way has CORS headers enabled). I disabled some aspects of web security with "LocalContentCanAccessRemoteUrls" (will attach full code below), and the XHR request work now, but the images are still not loaded. Any clues how to allow it?

    For some other weird reason it works on my machine (where QT is installed) but does not work on 2 other windows machines I tested with (which do not have QT).

    Please help, googled this issue for 3 hours, tried stuff, all failed.

    index.html:
    @
    <!DOCTYPE html>
    <html>
    <head>
    <title>photogram</title>
    <meta name="viewport" content="target-densitydpi=device-dpi, width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui" />
    </head>
    <body>
    </body>
    [removed]
    var img = document.createElement('img');
    img.onload = function(evt) {
    alert('OK');
    };
    img.onerror = function(evt) {
    alert('FAIL');
    };
    img.crossOrigin = ''; // no credentials flag. Same as img.crossOrigin='anonymous'
    img.src = 'http://d392hy3kg8y0uk.cloudfront.net/krpano/000000064/l1_f_1_1.jpg';

    window.document.body.appendChild(img&#41;;
    

    [removed]
    </html>
    @

    window.cpp
    @
    #include <QWebElement>
    #include <QWebFrame>
    #include <QStandardPaths>
    #include <QWebSecurityOrigin>
    #include "window.h"

    Window::Window(QWidget *parent)
    : QWidget(parent)
    {
    setupUi(this);
    }

    void Window::setUrl(const QUrl &url)
    {
    //
    QWebSecurityOrigin origin = webView->page()->mainFrame()->securityOrigin();
    origin.addAccessWhitelistEntry("http:", "*.cloudfront.net", QWebSecurityOrigin::AllowSubdomains);

    webView->setUrl(url);
    

    }

    void Window::setSettings()
    {

    //Context menu
    webView->page()->action(QWebPage::Reload)->setVisible(false);
    webView->page()->action(QWebPage::Back)->setVisible(false);
    
    //Remote access
    webView->settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);
    //webView->settings()->setAttribute(QWebSettings::LocalContentCanAccessFileUrls, true);
    
    //Storage
    webView->settings()->setLocalStoragePath(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation));
    webView->settings()->setOfflineStoragePath(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation));
    //webView->settings()->setOfflineWebApplicationCachePath(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation));
    
    webView->settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
    webView->settings()->setAttribute(QWebSettings::PrivateBrowsingEnabled, false);
    

    }
    @

    main.cpp
    @
    #include <QtWidgets>
    #include "window.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    Window window;

    window.setSettings();
    window.setUrl(QUrl::fromLocalFile&#40;QDir::current(&#41;.absoluteFilePath("public/index.html"&#41;));
    //
    
    window.show();
    return app.exec&#40;&#41;;
    

    }
    @



  • Then check that you deploy all required plugins like e.g. "the bearer plugins":http://qt-project.org/forums/viewthread/46576/



  • I works! thanks a TON.



  • Can you edit your first post please and add [SOLVED] for other users to know that this has been solved and a solution has been found


Log in to reply
 

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