在 Pyqt5 QWebEngineView 中单击下载绘图图标时有关绘图的问题



我创建了一个从QWebEngineView扩展的类,并声明了一个modeBar配置,如以下代码:

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()

但是当我单击下载绘图图标时,这不起作用,我在桌面上找不到保存的图像 我该如何解决问题。

发生这种情况是因为下载是由Web浏览器进行的,在本例中是QWebEngine。您需要确定如何处理 PyQt5 中的下载内容。

一种方法是:

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

其中_on_downloadFunc是决定如何处理您的下载的功能。它可能是:

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()

最新更新