furas

PyQt5 - QWebEngine - download file

Jul 21st, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. #
  2. # you can test it with local web server
  3. #
  4. # python -m http.server
  5. #
  6. # and it should have url http://localhost:8000
  7. #
  8.  
  9. import sys
  10. import PyQt5.Qt
  11.  
  12.  
  13. def download(item): # QWebEngineDownloadItem
  14.     print('downloading to', item.path())
  15.     item.accept() # accept file and it will download it (without any messages)
  16.  
  17.  
  18. app = PyQt5.Qt.QApplication(sys.argv)
  19. win = PyQt5.Qt.QWebEngineView()
  20.  
  21. # show page title as window title
  22. win.loadFinished.connect(lambda:win.setWindowTitle(win.title()))
  23.  
  24. # assign function which will download clicked files
  25. #profile = PyQt5.Qt.QWebEngineProfile.defaultProfile()
  26. #profile.downloadRequested.connect(download)
  27. win.page().profile().downloadRequested.connect(download)
  28.  
  29. #url = PyQt5.Qt.QUrl('https://google.com')
  30. url = PyQt5.Qt.QUrl('http://localhost:8000')
  31. win.load(url)
  32.  
  33. win.show()
  34.  
  35. app.exec_()
Add Comment
Please, Sign In to add comment