pyQT QNetworkManager 和 ProgressBars [英] pyQT QNetworkManager and ProgressBars

查看:37
本文介绍了pyQT QNetworkManager 和 ProgressBars的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一些代码,从网络服务器下载文件并保存它,在 QProgressBar 中显示下载进度.现在,有一些方法可以在常规 Python 中做到这一点,而且很容易.问题是它锁定了progressBar 的刷新.解决方案是使用 PyQT 的 QNetworkManager 类.我可以用它下载东西,但我无法在progressBar 上设置显示进度.举个例子:

I'm trying to code something that downloads a file from a webserver and saves it, showing the download progress in a QProgressBar. Now, there are ways to do this in regular Python and it's easy. Problem is that it locks the refresh of the progressBar. Solution is to use PyQT's QNetworkManager class. I can download stuff just fine with it, I just can't get the setup to show the progress on the progressBar. Here´s an example:

class Form(QDialog):

    def __init__(self,parent=None):
        super(Form,self).__init__(parent)
        self.progressBar = QProgressBar()
        self.reply = None
        layout = QHBoxLayout()
        layout.addWidget(self.progressBar)
        self.setLayout(layout)
        self.manager = QNetworkAccessManager(self)
        self.connect(self.manager,SIGNAL("finished(QNetworkReply*)"),self.replyFinished)
        self.Down()

    def Down(self):

        address = QUrl("http://stackoverflow.com") #URL from the remote file.
        self.manager.get(QNetworkRequest(address))
    def replyFinished(self, reply):
        self.connect(reply,SIGNAL("downloadProgress(int,int)"),self.progressBar, SLOT("setValue(int)"))
        self.reply = reply
        self.progressBar.setMaximum(reply.size())
        alltext = self.reply.readAll()
        #print alltext
        #print alltext
    def updateBar(self, read,total):
        print "read", read
        print "total",total
        #self.progressBar.setMinimum(0)
        #self.progressBar.setMask(total)
        #self.progressBar.setValue(read)

<小时>

在这种情况下,我的方法updateBar"永远不会被调用......有什么想法吗?


In this case, my method "updateBar" is never called... any ideas?

推荐答案

好吧,您还没有将任何信号连接到 updateBar() 方法.

Well you haven't connected any of the signals to your updateBar() method.

改变

def replyFinished(self, reply):
        self.connect(reply,SIGNAL("downloadProgress(int,int)"),self.progressBar, SLOT("setValue(int)"))

def replyFinished(self, reply):
        self.connect(reply,SIGNAL("downloadProgress(int,int)"),self.updateBar)

请注意,在 Python 中您不必显式使用 SLOT() 语法;您可以将引用传递给您的方法或函数.

Note that in Python you don't have to explicitly use the SLOT() syntax; you can just pass the reference to your method or function.

更新:

我只想指出,如果您想在处理期间 GUI 锁定的任何情况下使用进度条,一种解决方案是在另一个线程中运行您的处理代码,以便您的 GUI 接收重绘事件.考虑阅读有关 QThread 类的信息,以防您遇到进度条没有预先构建的解决方案的另一个原因.

I just wanted to point out that if you want to use a Progress bar in any situation where your GUI locks up during processing, one solution is to run your processing code in another thread so your GUI receives repaint events. Consider reading about the QThread class, in case you come across another reason for a progress bar that does not have a pre-built solution for you.

这篇关于pyQT QNetworkManager 和 ProgressBars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆