RuntimeError:QWebEngineDownloadItem 类型的包装 C/C++ 对象已在 PyQt 中删除 [关闭]

问题描述 投票:0回答:0

我写了一个网络浏览器。我想要做的是当我点击一个下载链接时,一个新窗口将打开来管理下载并在新窗口(下载管理器窗口)中管理开始下载和停止下载的任务。

当我点击下载链接时,会打开一个新窗口(下载管理器窗口)。但是,当我单击下载管理器窗口中的开始按钮时,出现此错误

RuntimeError:QWebEngineDownloadItem 类型的包装 C/C++ 对象已被删除

我的代码:

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
import os
import sys
from functools import partial
 

class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.browser = QWebEngineView()
        self.browser.page().profile().downloadRequested.connect(self.openDownloadManagerWindow)
        self.browser.setUrl(QUrl("http://google.com"))
        self.setCentralWidget(self.browser)
        navtb = QToolBar("Navigation")
        self.addToolBar(navtb)
        self.urlbar = QLineEdit()
        self.urlbar.returnPressed.connect(self.navigate_to_url)
        navtb.addWidget(self.urlbar)
        self.show()
 
    
    #download manager window
    def openDownloadManagerWindow(self , download):
        self.downloadWindow = QDialog()
        self.downloadWindow.resize(400,200)
        self.start_button = QPushButton("Start" , self.downloadWindow)
        self.stop_button = QPushButton("Stop" , self.downloadWindow)
        self.stop_button.move(320,0)
        self.downloadWindow.show()

        self.start_button.clicked.connect(partial(self.start , download))

    def start(self , download):
        download.accept()

    def navigate_to_url(self):
        q = QUrl(self.urlbar.text())
        if q.scheme() == "":
            q.setScheme("http")
        self.browser.setUrl(q)

 

app = QApplication(sys.argv)
window = MainWindow()
app.exec_()
python pyqt
© www.soinside.com 2019 - 2024. All rights reserved.