Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved How can i set baseUrl only once?

    Qt for Python
    2
    2
    98
    Loading More Posts
    • 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.
    • D
      deleted256 last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • eyllanesc
        eyllanesc last edited by

        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, ...):
                 super().__init(...)
                 self._base_url = QUrl()
                 self.base_url = QUrl.fromLocalFile(os.getcwd()+os.path.sep)
                 self.webPage = ...
        
            @property
            def base_url(self):
                return self._base_url
        
            @base_url.setter
            def base_url(self, url):
                self._base_url = url
        
            def update_html(self, html):
                self.webPage.setHtml(html, self.base_url)
        

        then:

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

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        1 Reply Last reply Reply Quote 0
        • First post
          Last post