小飞友 2022-04-02 20:45 采纳率: 60%
浏览 160
已结题

使用QThread编写多线程下载器时出现问题

图片:

img

报错信息如下:
<dir style="color:red"> QObject::setParent: Cannot set parent, new parent is in a different thread
QWidget::repaint: Recursive repaint detected
QBackingStore::endPaint() called with active painter; did you forget to destroy it or call QPainter::end() on it?
QBackingStore::endPaint() called with active painter; did you forget to destroy it or call QPainter::end() on it?
QBackingStore::endPaint() called with active painter; did you forget to destroy it or call QPainter::end() on it?
</dir>
调用时代码如下:
class RunThread(QThread):
        sig=pyqtSignal(str)
        def __init__(self):
            super(RunThread,self).__init__()
        def __del__(self):
            self.wait()
        def run(self):
            main.dlButton.update()
            logging.info('Download Button:clicked.')
            try:
                main.pbar.setValue(0)
                main.url=str(main.URL_lineEdit.text())
                main.filename=str(main.Filename_lineEdit.text())
                main.filepath=str(main.Path_lineEdit.text())
                if not os.path.exists(main.filepath):
                    os.makedirs(main.filepath)
                head=requests.head(main.url,headers=main.headers)
                content=head.content
                main.filesize=int(head.headers.get('Content-Length'))
                #https://issuecdn.baidupcs.com/issue/netdisk/yunguanjia/BaiduNetdisk_7.2.8.9.exe|self.filepath+self.filename
                response=requests.get(main.url,headers=main.headers,stream=True)
                read=0
                file_k_size=1024*2
                with open(main.filepath+'/'+main.filename,'wb') as write_file:
                    for chunk in response.iter_content(chunk_size=file_k_size):
                        read+=file_k_size
                        read=min(read,main.filesize)
                        write_file.write(chunk)
                        main.pbar.setValue(int((read/main.filesize)*100))
                        main.pbar.update()
                main.pbar.setValue(0)
                main.statusBar_.showMessage('下载完成')
            except Exception as exception:
                logging.exception(exception)
                subprocess.Popen('BugReport.pyw',shell=True)

运行时没有问题,但是偶尔有一两次未响应。
我的程序有logging记录的功能,但是貌似没什么用,没有记录到这个错误。
日志如下:

<dir style="color:green"> [2022-04-02 17:38:20,150] - [Downloader.pyw] - [11232] -[line:29] - [LEVEL:DEBUG] , In root: Log Start.
[2022-04-02 17:38:20,617] - [Downloader.pyw] - [11232] -[line:63] - [LEVEL:INFO] , In root: Icon loaded.
[2022-04-02 17:38:20,674] - [Downloader.pyw] - [11232] -[line:112] - [LEVEL:INFO] , In root: Ready.
[2022-04-02 17:38:20,884] - [Downloader.pyw] - [11232] -[line:179] - [LEVEL:DEBUG] , In root: Temp file deleted.
[2022-04-02 17:38:35,642] - [Downloader.pyw] - [11232] -[line:121] - [LEVEL:INFO] , In root: FileDialog Button:clicked.
[2022-04-02 17:38:38,804] - [Downloader.pyw] - [11232] -[line:148] - [LEVEL:INFO] , In root: Download Button:clicked. [2022-04-02 17:38:38,812] - [connectionpool.py] - [11232] -[line:1001] - [LEVEL:DEBUG] , In urllib3.connectionpool: Starting new HTTPS connection (1): issuecdn.baidupcs.com:443
[2022-04-02 17:38:40,948] - [connectionpool.py] - [11232] -[line:456] - [LEVEL:DEBUG] , In urllib3.connectionpool: https://issuecdn.baidupcs.com:443 "HEAD /issue/netdisk/yunguanjia/BaiduNetdisk_7.2.8.9.exe HTTP/1.1" 200 0
[2022-04-02 17:38:40,951] - [connectionpool.py] - [11232] -[line:1001] - [LEVEL:DEBUG] , In urllib3.connectionpool: Starting new HTTPS connection (1): issuecdn.baidupcs.com:443
[2022-04-02 17:38:41,061] - [connectionpool.py] - [11232] -[line:456] - [LEVEL:DEBUG] , In urllib3.connectionpool: https://issuecdn.baidupcs.com:443 "GET /issue/netdisk/yunguanjia/BaiduNetdisk_7.2.8.9.exe HTTP/1.1" 200 67765560
</dir>

我认为是在另一个类里调用了进度条的update()方法,我把这一行去掉,也没有闪退的情况了,但是任然会报一个错:

<dir style="color:red"> QObject::setParent: Cannot set parent, new parent is in a different thread </dir>

但是这不影响正常使用。
有个问题,就是我把update()去掉以后,进度条就没有之前那样丝滑的动画了,我希望保留。

请问该怎么解决?

  • 写回答

1条回答 默认 最新

  • 陈年椰子 2022-04-03 17:33
    关注

    看起来有点像主线程关闭了,但子线程还没终止, 子线程还在改变修改主窗口的显示。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月19日
  • 已采纳回答 4月11日
  • 修改了问题 4月3日
  • 修改了问题 4月2日
  • 展开全部

悬赏问题

  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题