【Pyqt4 实例21】实现http下载
来源:互联网 发布:java工厂方法设计模式 编辑:程序博客网 时间:2024/06/04 19:15
# -*- coding: utf-8 -*-from PyQt4.QtGui import *from PyQt4.QtCore import *import sysfrom PyQt4.QtNetwork import *QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))class HttpClient(QDialog): def __init__(self,parent=None): super(HttpClient,self).__init__(parent) self.setWindowTitle(self.tr("实现Http文件下载")) vbMain = QVBoxLayout(self) hb1 = QHBoxLayout() LabelName = QLabel(self) LabelName.setText(self.tr("地址:")) self.LineEditUser = QLineEdit(self) hb1.addWidget(LabelName) hb1.addWidget(self.LineEditUser) self.progressBar=QProgressBar() hb2 = QHBoxLayout() hb2.addWidget(self.progressBar) hb3 = QHBoxLayout() self.PushButtonDown = QPushButton(self) self.PushButtonDown.setText(self.tr("下载")) self.PushButtonCancel = QPushButton(self) self.PushButtonCancel.setText(self.tr("取消")) self.PushButtonExit = QPushButton(self) self.PushButtonExit.setText(self.tr("退出")) hb3.addWidget(self.PushButtonDown) hb3.addWidget(self.PushButtonCancel) hb3.addWidget(self.PushButtonExit) vbMain.addLayout(hb1) vbMain.addLayout(hb2) vbMain.addLayout(hb3) self.httpClient = QHttp(self) self.connect(self.httpClient,SIGNAL("requestFinished(int,bool)"),self.httpRequestFinished) self.connect(self.httpClient,SIGNAL("dataReadProgress(int,int)"),self.httpDataReadProcess) self.connect(self.httpClient,SIGNAL("responseHeaderReceived()"),self.httpResponseHeaderReceived) self.connect(self.PushButtonCancel,SIGNAL("clicked()"),self.slotCancel) self.connect(self.PushButtonDown,SIGNAL("clicked()"),self.slotDownload) self.connect(self.PushButtonExit,SIGNAL("clicked()"),self.slotExit) self.PushButtonCancel.setEnabled(False)
def slotDownload(self): url = QUrl(self.LineEditUser.text()) fileInfo = QFileInfo(url.path()) fileName = fileInfo.fileName() if QFile.exists(fileName): QMessageBox.information(self,self.tr("Error"),self.tr("File already exists!")) return self.file = QFile(fileName) if not self.file.open(QIODevice.WriteOnly): QMessageBox.information(self,self.tr("Error"),self.tr("Save file error!")) self.file = 0 return url_port = url.port() if url_port == -1: url_port = 80 self.httpClient.setHost(url.host(),url_port) if not url.userName().isEmpty(): self.httpClient.setUser(url.userName(),url.userPassword()) self.httpRequestAborted = False self.requestId = self.httpClient.get(url.path(),self.file) self.progressBar.reset() self.PushButtonDown.setEnabled(False) self.PushButtonCancel.setEnabled(True) def httpRequestFinished(self,id,error): if self.httpRequestAborted: if self.file: self.file.close() self.file.remove() self.file = 0 self.progressBar.reset() return if id == self.requestId: self.progressBar.reset() self.file.close() if error: self.file.remove() QMessageBox.information(self,self.tr("Error"),self.tr("Download failed!")) self.PushButtonDown.setEnabled(True) self.PushButtonCancel.setEnabled(False) self.file = 0 def httpDataReadProcess(self,done,total): self.progressBar.setMaximum(total) self.progressBar.setValue(done) def httpResponseHeaderReceived(self,responseHeader): if responseHeader.statusCode != 200 : QMessageBox.infomation(self,self.tr("Error"),self.tr("Download error!")) self.httpRequestAborted = True self.progressBar.reset() self.httpClient.abort() return def slotCancel(self): self.httpRequestAborted = True self.httpClient.abort() self.PushButtonDown.setEnabled(True) self.PushButtonCancel.setEnabled(False) def slotExit(self): self.close()app=QApplication(sys.argv)dialog=HttpClient()dialog.show()app.exec_()
0 0
- 【Pyqt4 实例21】实现http下载
- 【PyQt4 实例22】实现ftp的上传和下载
- PyQt4 精彩实例分析* 实例6 实现QQ抽屉效果
- *PyQt4 精彩实例分析* 实例6 实现QQ抽屉效果
- PyQt4 精彩实例分析* 实例6 实现QQ抽屉效果
- PyQt4 精彩实例分析* 实例21 不规则窗体
- PyQt4 实例源码阅读
- pyqt4实例练习1
- pyqt4实例练习2
- Java实现Http下载
- HTTP下载实现
- 实现http文件下载
- .net 实现http下载
- 【PyQt4实例9】橡皮筋线
- 【PyQt4实例10】地图浏览器
- 【PyQt4 实例24】文件浏览器
- 【PyQt4 实例27】事件过滤器
- PyQT4图形界面开发入门实例
- 在android编程中出现R cannot be resolved to a variable
- GDB+GdbServer: ARM程序调试
- 去除织梦DedeCMS列表和文章分页标签源码中的<li>
- Julius搭建语音识别库
- 二叉树非递归后序遍历
- 【Pyqt4 实例21】实现http下载
- 几类数据结构
- android 4.4 按键分析三
- DedeCMS漏洞之mysql_error_trace.inc日志信息
- Step By Step(C++模板类)
- 第七周项目二电阻并联
- 点双联通+缩点+LCA(HDU3686)
- Linux下交叉编译gdb和gdbserver
- vb 控制系统音量