2011-07-27 28 views
3

私のGUIで私は間にたくさんのものをダウンロードする必要があります。私はそれを行うためにurllibを使用します。もちろん、問題はGUIがすべてがダウンロードされるまでフリーズすることになります。pyqt4のスレッディング

私のコードは、上記の機能は、ダウンロードコードを有することを特徴と

QtCore.QObject.connect(self.UI.commandLinkButton_2 , QtCore.SIGNAL("clicked()") , self.addStoryToHistory) 

を、以下のようなものです。

この中に共有データを送信するのと同じように、プロセスにはデータをある場所にダウンロードするだけの処理はありません。

GUIをフリーズする最も簡単な方法は何ですか? multiprocessingまたはQThreadsを使用する必要がありますか?

誰もがいくつかのリンクに私を指すことができます....私は....任意の簡単な方法がある場合は、それはそれを指摘してくださいますので、それは非常に複雑であることが

おかげで多くのことを望んでいません..

答えて

5

は、私はちょうど私が戻っベースとしてのPyQtからのHTTPの例を使用して数ヶ月に取り組んでいたプロジェクトから取り除かました例です。 RiverbankのウェブサイトからSIPをダウンロードします。

これはurllibの代わりにQtNetworkのQHttpを使用しており、プログレスバーはdataReadProgressシグナルに接続されています。これにより、ファイルを確実にダウンロードできるだけでなく、応答性の高いGUIが得られるはずです。

from PyQt4.QtCore import QUrl, QFileInfo, QFile, QIODevice 
from PyQt4.QtGui import QApplication, QDialog, QProgressBar, QLabel, QPushButton, QDialogButtonBox, \ 
        QVBoxLayout, QMessageBox 
from PyQt4.QtNetwork import QHttp 

url_to_download = 'http://www.riverbankcomputing.co.uk/static/Downloads/sip4/sip-4.12.3.zip' 

class Downloader(QDialog): 
    def __init__(self, parent=None): 
     super(Downloader, self).__init__(parent) 

     self.httpGetId = 0 
     self.httpRequestAborted = False 
     self.statusLabel = QLabel('Downloading %s' % url_to_download) 
     self.closeButton = QPushButton("Close") 
     self.closeButton.setAutoDefault(False) 
     self.progressBar = QProgressBar() 

     buttonBox = QDialogButtonBox() 
     buttonBox.addButton(self.closeButton, QDialogButtonBox.RejectRole) 

     self.http = QHttp(self) 
     self.http.requestFinished.connect(self.httpRequestFinished) 
     self.http.dataReadProgress.connect(self.updateDataReadProgress) 
     self.http.responseHeaderReceived.connect(self.readResponseHeader) 
     self.closeButton.clicked.connect(self.cancelDownload) 

     mainLayout = QVBoxLayout() 
     mainLayout.addWidget(self.statusLabel) 
     mainLayout.addWidget(self.progressBar) 
     mainLayout.addWidget(buttonBox) 
     self.setLayout(mainLayout) 

     self.setWindowTitle('Download Example') 
     self.downloadFile() 

    def downloadFile(self): 
     url = QUrl(url_to_download) 
     fileInfo = QFileInfo(url.path()) 
     fileName = fileInfo.fileName() 

     if QFile.exists(fileName): 
      QFile.remove(fileName) 

     self.outFile = QFile(fileName) 
     if not self.outFile.open(QIODevice.WriteOnly): 
      QMessageBox.information(self, 'Error', 
        'Unable to save the file %s: %s.' % (fileName, self.outFile.errorString())) 
      self.outFile = None 
      return 

     mode = QHttp.ConnectionModeHttp 
     port = url.port() 
     if port == -1: 
      port = 0 
     self.http.setHost(url.host(), mode, port) 
     self.httpRequestAborted = False 

     path = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") 
     if path: 
      path = str(path) 
     else: 
      path = '/' 

     # Download the file. 
     self.httpGetId = self.http.get(path, self.outFile) 

    def cancelDownload(self): 
     self.statusLabel.setText("Download canceled.") 
     self.httpRequestAborted = True 
     self.http.abort() 
     self.close() 

    def httpRequestFinished(self, requestId, error): 
     if requestId != self.httpGetId: 
      return 

     if self.httpRequestAborted: 
      if self.outFile is not None: 
       self.outFile.close() 
       self.outFile.remove() 
       self.outFile = None 
      return 

     self.outFile.close() 

     if error: 
      self.outFile.remove() 
      QMessageBox.information(self, 'Error', 
        'Download failed: %s.' % self.http.errorString()) 

     self.statusLabel.setText('Done')  

    def readResponseHeader(self, responseHeader): 
     # Check for genuine error conditions. 
     if responseHeader.statusCode() not in (200, 300, 301, 302, 303, 307): 
      QMessageBox.information(self, 'Error', 
        'Download failed: %s.' % responseHeader.reasonPhrase()) 
      self.httpRequestAborted = True 
      self.http.abort() 

    def updateDataReadProgress(self, bytesRead, totalBytes): 
     if self.httpRequestAborted: 
      return 
     self.progressBar.setMaximum(totalBytes) 
     self.progressBar.setValue(bytesRead) 

if __name__ == '__main__': 
    import sys 
    app = QApplication(sys.argv) 
    downloader = Downloader() 
    downloader.show() 
    sys.exit(app.exec_()) 
+0

こんにちはありがとうございました...私はバッチダウンロードをしたいと思ったら、これでダウンロードプロセス全体で同じプログレスバーを使うことができますか? –

+0

申し訳ありませんが、私はこのコメントを逃した。私はこれがどのようにファイルとファイルのサイズかもしれないかによって決まると言いたい。小規模で多くの場合、ファイル数がプログレスバーの最大値を持ち、ファイルがダウンロードされると同時に移動します。ファイルサイズが変わる場合は、より正確な表示のために、ダウンロードする前にすべてのファイルの合計サイズを取得して、プログレスバーを設定する必要があります。 –