Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Search Gists
Sign in Sign up

Instantly share code, notes, and snippets.

@bootchk
CreatedFebruary 15, 2014 21:40
    • Star(5)You must be signed in to star a gist
    • Fork(1)You must be signed in to fork a gist
    Save bootchk/9025575 to your computer and use it in GitHub Desktop.
    A class for asynchronous url loading using Qt, PyQt
    '''
    Copyright 2014 Lloyd Konneker
    Release under the GPLv3
    '''
    fromPyQt5.QtCoreimportpyqtSignalasSignal
    fromPyQt5.QtCoreimportQObject,QByteArray,QUrl
    fromPyQt5.QtNetworkimportQNetworkAccessManager,QNetworkRequest
    classDownLoader(QObject):
    '''
    Asynchronous download from network, which is expected to be unreliable and possibly slow.
    A thin wrapper around QNetworkAccessManager()
    Qt docs: 'One QNetworkAccessManager should be enough for the whole Qt application.'
    Similarly, one DownLoader might be enought for the whole app.
    It is untested what happens when you create more than one.
    Usage:
    foo = Downloader(url)
    foo.downloaded.connect(clientLoader)
    foo.doDownload(QUrl('http:/...'))
    # execution continues, clientLoader slot will receive signal
    def clientLoader():
    bar = foo.downloadedData()
    # bar is only a reference, consume it before calling doDownload() again
    '''
    downloaded=Signal()
    def__init__(self):# parent not used
    super(DownLoader,self).__init__()# !!! init QObject
    # private
    self._webController=QNetworkAccessManager()
    self._downloadedData=None
    # connect asynchronous result, when a request finishes
    self._webController.finished.connect(self._fileDownloaded)
    # private slot, no need to declare as slot
    def_fileDownloaded(self,reply):
    '''
    Handle signal 'finished'. A network request has finished.
    '''
    self._downloadedData=reply.readAll()
    # prior _downloadedData is now garbage collectable
    assertisinstance(self._downloadedData,QByteArray)
    reply.deleteLater()# schedule for delete from main event loop
    # print("emitted")
    self.downloaded.emit()
    '''
    Public API
    '''
    defdoDownload(self,url):
    assertisinstance(url,QUrl)
    request=QNetworkRequest(url)
    self._webController.get(request)
    # asynchronous, does not wait, execution continues
    defdownloadedData(self):
    '''
    QByteArray that was downloaded.
    Call this only after receiving signal 'downloaded'.
    Copy result before calling doDownload() again.
    '''
    returnself._downloadedData
    Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

    [8]ページ先頭

    ©2009-2025 Movatter.jp