Qt World Summit: Submit your Presentation

How can i set baseUrl only once?

  • I found the following code for rendering images from relative paths in HTML

    self.webPage.setHtml('<img src="image.png"/>', baseUrl=QUrl.fromLocalFile(os.getcwd()+os.path.sep))

    However i call setHTML a lot, and would rather be able to set baseURL once rather than adding it as an argument each time. How can i do this?

  • You always have to set the baseUrl if you want to use setHtml, but if you want to repeat the code then create a method that does it:

    class Foo(Bar):
        def __init__(self, ...):
             self._base_url = QUrl()
             self.base_url = QUrl.fromLocalFile(os.getcwd()+os.path.sep)
             self.webPage = ...
        def base_url(self):
            return self._base_url
        def base_url(self, url):
            self._base_url = url
        def update_html(self, html):
            self.webPage.setHtml(html, self.base_url)


    self.update_html('<img src="image.png"/>');

Log in to reply