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

使用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 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题