Thanks to visit codestin.com
Credit goes to sourceforge.net

Menu

[r253]: / trunk / QDownloader.py  Maximize  Restore  History

Download this file

48 lines (43 with data), 1.7 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtNetwork import QHttp
class QDownloader(QWidget):
def __init__(self, parent=None):
QTabWidget.__init__(self, parent)
self.parent = parent
self.url = ""
self.downloadPath = ""
self.qhttp = QHttp(self)
self.progressbar = QProgressBar(self)
self.cancel = QPushButton("Cancel", self)
self.layout = QHBoxLayout(self)
self.layout.addWidget(self.progressbar)
self.layout.addWidget(self.cancel)
self.show()
self.resize(self.size().width()*2, self.size().height())
self.connect(self.cancel, QtCore.SIGNAL("clicked()"), self.cancelDownload)
self.connect(self.qhttp, QtCore.SIGNAL("requestFinished(int, bool)"), self.httpRequestFinished)
self.connect(self.qhttp, QtCore.SIGNAL("dataReadProgress(int, int)"), self.updateDataReadProgress)
def download(self):
url = QUrl(self.url)
self.qhttp.setHost(url.host())
url = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/")
self.downloadPathName = QFile(self.downloadPath)
self.downloadPathName.open(QIODevice.WriteOnly)
self.downloadID = self.qhttp.get(QString(url), self.downloadPathName)
def httpRequestFinished(self, requestID, error):
if not requestID == self.downloadID:
return
self.downloadPathName.close()
self.close()
def updateDataReadProgress(self, done, total):
self.progressbar.setMaximum(total)
self.progressbar.setValue(done)
def cancelDownload(self):
self.qhttp.abort()
self.downloadPathName.close()
self.downloadPathName.remove()
self.close()