pyQT QNetworkManager 和 ProgressBars

发布于 2024-07-17 10:18:53 字数 1369 浏览 5 评论 0原文

我正在尝试编写一些代码,从网络服务器下载文件并保存它,在 QProgressBar 中显示下载进度。 现在,有多种方法可以在常规 Python 中执行此操作,而且很简单。 问题是它锁定了进度条的刷新。 解决方案是使用 PyQT 的 QNetworkManager 类。 我可以用它下载东西,只是无法设置在进度条上显示进度。 这是一个例子:

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”从未被调用......有什么想法吗?

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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃→可爱长大的 2024-07-24 10:18:53

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

更改

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() 语法; 您只需传递对您的方法或函数的引用即可。

更新:

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

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

change

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

to

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

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.

Update:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文