0

I created a class extended from QWebEngineView and declared a modeBar configuration as following code:

class Canvas(QWebEngineView):
    def __init__(self, parent: QFrame = None):
        super(QWebEngineView, self).__init__()
        dir = Path(environ['USERPROFILE']) / 'Desktop'
        self.mode_bar_config = {
            'toImageButtonOptions': {
                'format': 'jpeg',  # one of png, svg, jpeg, webp
                'filename': (dir / ('test_img_' +
                            datetime.now().strftime('%Y%m%d%H%M%S')))
                            .as_posix(),
                'height': 400,
                'width': 650,
                'scale': 1,
                'modeBarButtonsToRemove': ['toggleSpikelines']
            }
        }
        self.parent = parent
        self.update_canvas()

    def update_canvas(self, list_of_data: list = None):
       try:
          if list_of_data is None:
             list_of_data = []

          self.fig1 = FftFigure('Time domain',
                                        x_title='sample points',
                                        y_title='dbm')
          if len(list_of_data) > 0:
             x = [index for index in range(len(list_of_data))]
             self.fig1.update_data(x, y_data=list_of_data)
         
          html = '<html><head><meta charset="utf-8" />'
          html += '<script src="https://cdn.plot.ly/plotly-latest.min.js"></script></head>'
          html += '<body style="width:650px;height:700px;">'
          html += offline.plot(self.fig1, output_type='div',
                                       include_plotlyjs=False,
                                       config=self.mode_bar_config)
          html += '</body></html>'
          self.setHtml(html)
          layout = QVBoxLayout()
          layout.addWidget(self)
          self.parent.setLayout(layout)
          if len(list_of_data) > 0:
             self.render(self)
      except Exception as e:
         print_exc()

but this is not work when i clicked the download plot icon, i can't find the saved image on my Desktop How can i resolve the problem.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    Does this answer your question? [How to open download file dialog with QWebEngineView?](https://stackoverflow.com/questions/50164712) – eyllanesc Jul 09 '21 at 06:38

1 Answers1

1

This happens because the download is made by the web browser, in this case your QWebEngine. You need to determine what to do with your downloads inside of PyQt5.

A way to do so:

self.browser = QWebEngineView(self)  #"self" could be a QDialog class
self.browser.page().profile().downloadRequested.connect(self._on_downloaFunc)

where the _on_downloadFunc is the function which will determine what to do with your download. It could be:

def _on_downloadFunc(self, download):
    # save_file_name_dialog is a function to show the windows file window
    image_path = save_file_name_dialog(self, "Choose your file", "Png files (*.png)")
    if path:
        download.setPath(image_path)
        download.accept()