RuntimeError: wrapped C/C++ object of type QWebEngineDownloadItem has been deleted in PyQt

huangapple go评论47阅读模式
英文:

RuntimeError: wrapped C/C++ object of type QWebEngineDownloadItem has been deleted in PyQt

问题

I wrote a web browser

What I want to do is when I click on the download links, a new window will open to manage the downloads and manage the tasks of starting downloads and stopping downloads in a new window (download manager window)

When I click on the download links,

a new window (download manager window) opens But when I click on the start button in the download manager window, I get this error

RuntimeError: wrapped C/C++ object of type QWebEngineDownloadItem has been deleted

#my Code:

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_()
英文:

I wrote a web browser

What I want to do is when I click on the download links, a new window will open to manage the downloads and manage the tasks of starting downloads and stopping downloads in a new window(download manager window)

When I click on the download links,

a new window (download manager window) opens But when I click on the start button in the download manager window, I get this error

RuntimeError: wrapped C/C++ object of type QWebEngineDownloadItem has been deleted

#my Code:

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

答案1

得分: 1

根据文档
> 如果没有任何信号处理程序调用accept(),那么[QWebEngineDownloadItem]将在信号发射后立即被删除。这意味着应用程序不得保留对被拒绝的下载项的引用。

因此,在您的openDownloadManagerWindow中,您需要接受下载,否则它将在函数结束时被删除,并导致您的应用程序出现故障。

英文:

According to the documentation :
> If accept() is not called by any signal handler, then the [QWebEngineDownloadItem] will be deleted immediately after signal emission. This means that the application must not keep references to rejected download items.

So you need to accept the download in your openDownloadManagerWindow, since it will otherwise be deleted at the end of the function, and cause your application to break.

huangapple
  • 本文由 发表于 2023年6月16日 12:23:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定