关闭QWebEngineView警告“已请求发布配置文件,但WebEnginePage仍未删除。预计会出现问题!”

2024-04-19 19:59:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了如下所示的PlotlyViewer类来显示一个Plotly graph,它工作正常,但在我关闭它时会显示此警告:Release of profile requested but WebEnginePage still not deleted. Expect troubles !

当我在__init__中创建自己的QWebEngineProfile实例以便连接到downloadRequested信号以显示保存文件对话框时,警告开始出现。 我让我的PlotlyViewer成为QWebEnginePage的父对象,但是当父对象关闭时,它听起来好像没有得到清理?我不明白为什么

import os
import tempfile

from plotly.io import to_html
import plotly.graph_objs as go
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets, sip, QtWebEngineWidgets
from PyQt5.QtCore import Qt

class PlotlyViewer(QtWebEngineWidgets.QWebEngineView):
    def __init__(self, fig=None):
        super().__init__()

        # https://stackoverflow.com/a/48142651/3620725
        self.profile = QtWebEngineWidgets.QWebEngineProfile(self)
        self.page = QtWebEngineWidgets.QWebEnginePage(self.profile, self)
        self.setPage(self.page)
        self.profile.downloadRequested.connect(self.on_downloadRequested)

        # https://stackoverflow.com/a/8577226/3620725
        self.temp_file = tempfile.NamedTemporaryFile(mode="w", suffix=".html", delete=False)
        self.set_figure(fig)

        self.resize(700, 600)
        self.setWindowTitle("Plotly Viewer")

    def set_figure(self, fig=None):
        self.temp_file.seek(0)

        if fig:
            self.temp_file.write(to_html(fig, config={"responsive": True}))
        else:
            self.temp_file.write("")

        self.temp_file.truncate()
        self.temp_file.seek(0)
        self.load(QtCore.QUrl.fromLocalFile(self.temp_file.name))

    def closeEvent(self, event: QtGui.QCloseEvent) -> None:
        self.temp_file.close()
        os.unlink(self.temp_file.name)

    def sizeHint(self) -> QtCore.QSize:
        return QtCore.QSize(400, 400)

    # https://stackoverflow.com/questions/55963931/how-to-download-csv-file-with-qwebengineview-and-qurl
    def on_downloadRequested(self, download):
        dialog = QtWidgets.QFileDialog()
        dialog.setDefaultSuffix(".png")
        path, _ = dialog.getSaveFileName(self, "Save File", os.path.join(os.getcwd(), "newplot.png"), "*.png")
        if path:
            download.setPath(path)
            download.accept()

if __name__ == "__main__":
    app = QtWidgets.QApplication([])

    fig = go.Figure()
    fig.add_scatter(
        x=np.random.rand(100),
        y=np.random.rand(100),
        mode="markers",
        marker={
            "size": 30,
            "color": np.random.rand(100),
            "opacity": 0.6,
            "colorscale": "Viridis",
        },
    )

    pv = PlotlyViewer(fig)
    pv.show()
    app.exec_()

Tags: pathimportselfosdownloaddefnpfig
3条回答

问题是python处理内存的方式不符合Qt预先建立的规则(这就是绑定问题),也就是说,Qt希望先删除QWebEnginePage,但python先删除QWebEngineProfile

在您的情况下,我不认为需要创建与默认情况下不同的QWebEnginePage或QWebEngineProfile,但要在默认情况下获取QWebEngineProfile:

class PlotlyViewer(QtWebEngineWidgets.QWebEngineView):
    def __init__(self, fig=None):
        super().__init__()
        self.page().profile().downloadRequested.connect(self.on_downloadRequested)

        # https://stackoverflow.com/a/8577226/3620725
        self.temp_file = tempfile.NamedTemporaryFile(
            mode="w", suffix=".html", delete=False
        )
        self.set_figure(fig)

        self.resize(700, 600)
        self.setWindowTitle("Plotly Viewer")

相关问题 更多 >