tags:

views:

539

answers:

1

Hello, 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?

+1  A: 

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.

Chris Cameron