Results 1 to 5 of 5

Thread: Use QImage/QPixmap as source for HTML img elements in QWebEngine

  1. #1
    Join Date
    Nov 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Use QImage/QPixmap as source for HTML img elements in QWebEngine

    Hi.

    I have a QWebEngineView loading a html from Qt resources which works fine. I can also add images to the resources and use them as source for my <img> elements in the web page. My problem is that I can't dynamically add images to the webpage at runtime without using a horrible hack (see: http://stackoverflow.com/questions/6...object-in-html).

    The images I want to load are on the local system, so my first thought was to reference them using the file:// protocol, but this is not allowed.

    My second thought was to add images to the Qt resource system at runtime, but that doesen't seem to be possible either.

    Is there really no nice way to add new images to QWebEngine web pages?

    Best regards
    Jakob Simon-Gaarde

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Use QImage/QPixmap as source for HTML img elements in QWebEngine

    Have you tried enabling QWebEngineSettings::LocalContentCanAccessFileUrls?

    If that does not work, you could try this:

    - create a temporary directory using QTemporaryDir
    - extract the assets from the resource into that dir
    - then load the local file

    Since everything is local files, it should allow to reference other local files.
    In the worst case you need to copy the additional images to the same temporary directory.

    Cheers,
    _

  3. #3
    Join Date
    Nov 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Use QImage/QPixmap as source for HTML img elements in QWebEngine

    Thanks for the reply.

    I forgot to say that my main html-document is loaded from qrc which seems to be what is blocking the access to the local filesystem.

    I guess I'm forced to load my html from the filesystem then, or what?

    Cheers

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Use QImage/QPixmap as source for HTML img elements in QWebEngine

    Quote Originally Posted by Jakob View Post
    I forgot to say that my main html-document is loaded from qrc which seems to be what is blocking the access to the local filesystem.
    No, you already said that.

    Quote Originally Posted by Jakob View Post
    I guess I'm forced to load my html from the filesystem then, or what?
    See my other comment.

    Cheers,
    _

  5. #5
    Join Date
    Nov 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Use QImage/QPixmap as source for HTML img elements in QWebEngine

    ok, well then using the QWebEngineSettings::LocalContentCanAccessFileUrls attribute was not enough. Actually it seems be enough to extract the main html file to a local tempdir. I can still load other resources via qrc afterwards.

    I think it would be nice if it was possible to extend and reduce the resource dictionary runtime so you could access bytearrays using :/ and qrc:/.

    Ie:
    * QResource::addData(const QString &path, const QByteArray &data);
    * QResource::removeData(const QString &path);

    Cheers Jakob

Similar Threads

  1. Replies: 2
    Last Post: 18th September 2015, 09:40
  2. Hot do I get HTML source from a web page?
    By earendil in forum Newbie
    Replies: 9
    Last Post: 16th March 2012, 16:15
  3. Replies: 4
    Last Post: 13th February 2012, 19:09
  4. How to make custom menu for QWebView html elements ?
    By rsilva in forum Qt Programming
    Replies: 0
    Last Post: 10th May 2011, 01:18
  5. What's faster: QPixmap-to-QImage or QImage-to-QPixmap
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2006, 18:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.