0
from PyQt5.QtWebEngineWidgets import QWebEngineView
...
...
q = QWebEngineView()
q.setHtml('''<img border="0" height="200" src="E:/img.png" style="display: block; text-align: left;" width="600">''')
...
...

above is the only code I deal with webengineview. I want to show image within the webengineview, but it doesn't work, the outcome is a broken image icon.

when I open the html text with chrome browser, it actually works.

and then I set the img "src" attribute to be some other web image, it still works.

and it works if I set the src as "data:image/png;base64,xxxx" format.

I cannot figure out what happened

Arthur
  • 63
  • 1
  • 8
  • it's access to file system, because i load the html text from the file, then pass it to webengineview. the demo code is the reduced code with the same problem – Arthur Oct 25 '22 at 08:53
  • 1
    You need to use the `baseUrl` argument of [setHtml](https://doc.qt.io/qt-5/qwebengineview.html#setHtml). And note that it must be a *file://* url that includes a trailing slash: i.e. `baseUrl=QtCore.QUrl('file:///E:/'))`. If you do that, it will also be possible to use relative paths in the html. This applies to all external resources - not just images. – ekhumoro Oct 25 '22 at 19:54
  • @ekhumoro thanks for your advice. instead of setHtml(), I use setUrl() now, and it works – Arthur Oct 26 '22 at 03:12

1 Answers1

0

I suppose the issue might be connected to wrong path you specified. I assume you use Windows, and then the correct src would be E:\img.png (notice the slash has opposite direction).

  • I checked your solution but in vain. and I set the src to be file:///E:/img.png or file:\\\E:\img.png, it doesn't work either. – Arthur Oct 25 '22 at 08:55