Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. Qt WebKit crossOrigin restriction when loading images.
QtWS25 Last Chance

Qt WebKit crossOrigin restriction when loading images.

Scheduled Pinned Locked Moved Qt WebKit
4 Posts 3 Posters 2.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    iacovlev.pavel
    wrote on last edited by
    #1

    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;;
    

    }
    @

    1 Reply Last reply
    0
    • T Offline
      T Offline
      ThatDude
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • I Offline
        I Offline
        iacovlev.pavel
        wrote on last edited by
        #3

        I works! thanks a TON.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          arsinte_andrei
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved