Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. Saving images on QWebEngine
Forum Updated to NodeBB v4.3 + New Features

Saving images on QWebEngine

Scheduled Pinned Locked Moved Unsolved QtWebEngine
4 Posts 2 Posters 1.2k Views
  • 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 Offline
    H Offline
    Harborman
    wrote on last edited by
    #1

    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.

    eyllanescE 1 Reply Last reply
    0
    • H Harborman

      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.

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #2

      @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
      0
      • eyllanescE eyllanesc

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

        H Offline
        H Offline
        Harborman
        wrote on last edited by
        #3

        @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
        0
        • H Offline
          H Offline
          Harborman
          wrote on last edited by Harborman
          #4

          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
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved