本文整理汇总了Python中qgis.PyQt.QtCore.QUrl.path方法的典型用法代码示例。如果您正苦于以下问题:Python QUrl.path方法的具体用法?Python QUrl.path怎么用?Python QUrl.path使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.PyQt.QtCore.QUrl
的用法示例。
在下文中一共展示了QUrl.path方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: FileDownloader
# 需要导入模块: from qgis.PyQt.QtCore import QUrl [as 别名]
# 或者: from qgis.PyQt.QtCore.QUrl import path [as 别名]
class FileDownloader():
"""The blueprint for downloading file from url."""
def __init__(self, url, output_path, progress_dialog=None):
"""Constructor of the class.
.. versionchanged:: 3.3 removed manager parameter.
:param url: URL of file.
:type url: str
:param output_path: Output path.
:type output_path: str
:param progress_dialog: Progress dialog widget.
:type progress_dialog: QWidget
"""
# noinspection PyArgumentList
self.manager = QgsNetworkAccessManager.instance()
self.url = QUrl(url)
self.output_path = output_path
self.progress_dialog = progress_dialog
if self.progress_dialog:
self.prefix_text = self.progress_dialog.labelText()
self.output_file = None
self.reply = None
self.downloaded_file_buffer = None
self.finished_flag = False
def download(self):
"""Downloading the file.
:returns: True if success, otherwise returns a tuple with format like
this (QNetworkReply.NetworkError, error_message)
:raises: IOError - when cannot create output_path
"""
# Prepare output path
self.output_file = QFile(self.output_path)
if not self.output_file.open(QFile.WriteOnly):
raise IOError(self.output_file.errorString())
# Prepare downloaded buffer
self.downloaded_file_buffer = QByteArray()
# Request the url
request = QNetworkRequest(self.url)
self.reply = self.manager.get(request)
self.reply.readyRead.connect(self.get_buffer)
self.reply.finished.connect(self.write_data)
self.manager.requestTimedOut.connect(self.request_timeout)
if self.progress_dialog:
# progress bar
def progress_event(received, total):
"""Update progress.
:param received: Data received so far.
:type received: int
:param total: Total expected data.
:type total: int
"""
# noinspection PyArgumentList
QgsApplication.processEvents()
self.progress_dialog.adjustSize()
human_received = humanize_file_size(received)
human_total = humanize_file_size(total)
label_text = tr("%s : %s of %s" % (
self.prefix_text, human_received, human_total))
self.progress_dialog.setLabelText(label_text)
self.progress_dialog.setMaximum(total)
self.progress_dialog.setValue(received)
# cancel
def cancel_action():
"""Cancel download."""
self.reply.abort()
self.reply.deleteLater()
self.reply.downloadProgress.connect(progress_event)
self.progress_dialog.canceled.connect(cancel_action)
# Wait until finished
# On Windows 32bit AND QGIS 2.2, self.reply.isFinished() always
# returns False even after finished slot is called. So, that's why we
# are adding self.finished_flag (see #864)
while not self.reply.isFinished() and not self.finished_flag:
# noinspection PyArgumentList
QgsApplication.processEvents()
result = self.reply.error()
try:
http_code = int(self.reply.attribute(
QNetworkRequest.HttpStatusCodeAttribute))
#.........这里部分代码省略.........