Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    Unsolved Saving images on QWebEngine

    QtWebEngine
    2
    4
    389
    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.
    • H
      Harborman last edited by

      This should be one of the most basic things in a web browser but I can't find out how to do it.
      I have a custom context menu on my QWebEngine browser and I want to save online images to disk.
      I've already seen an example on how to save the entire html page to disk but that's rarely needed.
      Can this be done? The browser would first have to recognize all the images on the web page and get their URLs.

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

        @Harborman Use js(read about runJavaScript method), also read https://stackoverflow.com/a/9321881/6622587

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

        H 1 Reply Last reply Reply Quote 0
        • H
          Harborman @eyllanesc last edited by

          @eyllanesc said in Saving images on QWebEngine:

          @Harborman Use js(read about runJavaScript method), also read https://stackoverflow.com/a/9321881/6622587

          Thanks. This is currently outside my skill range but I'll look into it.
          I didn't even know you could mix JS with Python, I'm surprised.

          1 Reply Last reply Reply Quote 0
          • H
            Harborman last edited by Harborman

            Just wanted to add for future readers that I came up with a simple workaround:

            First you check in your browser's custom context menu if an image was right-clicked:

                    if self.page().contextMenuData().mediaType() == 1:  #test if an image was clicked (img = 1, video = 2, audio = 3)
                        self.menus = QMenu()
                        imgurl = self.page().contextMenuData().mediaUrl()
                        action1 = QAction("Save image to disk", self)
                        self.menus.addAction(action1)
                        self.menus.popup(event.globalPos())
                        action1.triggered.connect(lambda imgsave: self.saveimg(imgurl))
            
            

            This sends the image's url to a custom function that saves the image using Python's requests library.

                def saveimg(self, imgurl):
                    fileurl = imgurl.toString()  #needs to be string
                    response = requests.get(fileurl)
                    filename = fileurl.split("/")[-1]  #get filename from url
                    dlfolder = "downloads/"  #put your desired folder path here
                    file_path = os.path.join(dlfolder, filename)
                    open(file_path, "wb").write(response.content)
            
            
            1 Reply Last reply Reply Quote 0
            • First post
              Last post