本文整理汇总了Python中PyQt5.QtNetwork.QNetworkAccessManager.setCache方法的典型用法代码示例。如果您正苦于以下问题:Python QNetworkAccessManager.setCache方法的具体用法?Python QNetworkAccessManager.setCache怎么用?Python QNetworkAccessManager.setCache使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtNetwork.QNetworkAccessManager
的用法示例。
在下文中一共展示了QNetworkAccessManager.setCache方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SlippyMap
# 需要导入模块: from PyQt5.QtNetwork import QNetworkAccessManager [as 别名]
# 或者: from PyQt5.QtNetwork.QNetworkAccessManager import setCache [as 别名]
class SlippyMap(QObject):
updated = pyqtSignal(QRect)
def __init__(self, parent=None):
super(SlippyMap, self).__init__(parent)
self._offset = QPoint()
self._tilesRect = QRect()
self._tilePixmaps = {} # Point(x, y) to QPixmap mapping
self._manager = QNetworkAccessManager()
self._url = QUrl()
# public vars
self.width = 400
self.height = 300
self.zoom = 15
self.latitude = 59.9138204
self.longitude = 10.7387413
self._emptyTile = QPixmap(TDIM, TDIM)
self._emptyTile.fill(Qt.lightGray)
cache = QNetworkDiskCache()
cache.setCacheDirectory(
QStandardPaths.writableLocation(QStandardPaths.CacheLocation))
self._manager.setCache(cache)
self._manager.finished.connect(self.handleNetworkData)
def invalidate(self):
if self.width <= 0 or self.height <= 0:
return
ct = tileForCoordinate(self.latitude, self.longitude, self.zoom)
tx = ct.x()
ty = ct.y()
# top-left corner of the center tile
xp = int(self.width / 2 - (tx - math.floor(tx)) * TDIM)
yp = int(self.height / 2 - (ty - math.floor(ty)) * TDIM)
# first tile vertical and horizontal
xa = (xp + TDIM - 1) / TDIM
ya = (yp + TDIM - 1) / TDIM
xs = int(tx) - xa
ys = int(ty) - ya
# offset for top-left tile
self._offset = QPoint(xp - xa * TDIM, yp - ya * TDIM)
# last tile vertical and horizontal
xe = int(tx) + (self.width - xp - 1) / TDIM
ye = int(ty) + (self.height - yp - 1) / TDIM
# build a rect
self._tilesRect = QRect(xs, ys, xe - xs + 1, ye - ys + 1)
if self._url.isEmpty():
self.download()
self.updated.emit(QRect(0, 0, self.width, self.height))
def render(self, p, rect):
for x in range(self._tilesRect.width()):
for y in range(self._tilesRect.height()):
tp = Point(x + self._tilesRect.left(), y + self._tilesRect.top())
box = self.tileRect(tp)
if rect.intersects(box):
p.drawPixmap(box, self._tilePixmaps.get(tp, self._emptyTile))
def pan(self, delta):
dx = QPointF(delta) / float(TDIM)
center = tileForCoordinate(self.latitude, self.longitude, self.zoom) - dx
self.latitude = latitudeFromTile(center.y(), self.zoom)
self.longitude = longitudeFromTile(center.x(), self.zoom)
self.invalidate()
# slots
def handleNetworkData(self, reply):
img = QImage()
tp = Point(reply.request().attribute(QNetworkRequest.User))
url = reply.url()
if not reply.error():
if img.load(reply, None):
self._tilePixmaps[tp] = QPixmap.fromImage(img)
reply.deleteLater()
self.updated.emit(self.tileRect(tp))
# purge unused tiles
bound = self._tilesRect.adjusted(-2, -2, 2, 2)
for tp in list(self._tilePixmaps.keys()):
if not bound.contains(tp):
del self._tilePixmaps[tp]
self.download()
def download(self):
grab = None
for x in range(self._tilesRect.width()):
for y in range(self._tilesRect.height()):
tp = Point(self._tilesRect.topLeft() + QPoint(x, y))
if tp not in self._tilePixmaps:
#.........这里部分代码省略.........
示例2: MostWatchedItems
# 需要导入模块: from PyQt5.QtNetwork import QNetworkAccessManager [as 别名]
# 或者: from PyQt5.QtNetwork.QNetworkAccessManager import setCache [as 别名]
class MostWatchedItems(QWidget):
def __init__(self, parent=None):
super(MostWatchedItems, self).__init__(parent)
self.ui = Ui_MostWatchedItems()
self.ui.setupUi(self)
self.ui.tableWidget.setColumnCount(5)
self.ui.tableWidget.setColumnWidth(0, 160)
self.ui.tableWidget.setColumnWidth(1, 480)
self.categoryID = ''
self.manager = QNetworkAccessManager(self)
diskCache = QNetworkDiskCache(self)
diskCache.setCacheDirectory("cache")
self.manager.setCache(diskCache)
self.manager.finished.connect(self.on_finished)
self.manager.sslErrors.connect(self.on_sslErrors)
self.replyMap = dict()
@pyqtSlot(bool)
def on_selectPushButton_clicked(self, checked):
dialog = CategoryInfoDialog.instance()
res = dialog.exec()
if res == QDialog.Accepted:
self.ui.categoryLineEdit.setText('%s -> %s' % (dialog.categoryID(), dialog.categoryNamePath()))
self.categoryID = dialog.categoryID()
@pyqtSlot(bool)
def on_clearPushButton_clicked(self, checked):
self.ui.categoryLineEdit.setText('')
self.categoryID = ''
@pyqtSlot(bool)
def on_wipePushButton_clicked(self, checked):
while self.ui.tableWidget.rowCount() > 0:
self.ui.tableWidget.removeRow(0)
@pyqtSlot(bool)
def on_searchPushButton_clicked(self, checked):
if self.categoryID != '' and self.categoryID != '-1':
merchandising = Merchandising(warnings = False)
response = merchandising.execute(
'getMostWatchedItems',
{
'categoryId': self.categoryID,
'maxResults': self.ui.spinBox.value()
}
)
reply = response.reply
itemRecommendations = reply.itemRecommendations.item
row = self.ui.tableWidget.rowCount()
for item in itemRecommendations:
self.ui.tableWidget.insertRow(row)
imageUrl = 'http://thumbs3.ebaystatic.com/pict/%s4040.jpg' % item.itemId
request = QNetworkRequest(QUrl(imageUrl))
request.setAttribute(QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.PreferCache)
reply = self.manager.get(request)
self.replyMap[reply] = row
viewItemURL = QLabel()
viewItemURL.setOpenExternalLinks(True)
#viewItemURL.setTextInteractionFlags(Qt.TextBrowserInteraction)
title = '<a href="%s">%s</a>' % (item.viewItemURL, item.title)
viewItemURL.setText(title)
self.ui.tableWidget.setCellWidget(row, 1, viewItemURL)
self.ui.tableWidget.setItem(row, 2, QTableWidgetItem(item.primaryCategoryName))
self.ui.tableWidget.setItem(row, 3, QTableWidgetItem(item.buyItNowPrice.value))
self.ui.tableWidget.setItem(row, 4, QTableWidgetItem(item.watchCount))
row += 1
@pyqtSlot(QNetworkReply)
def on_finished(self, reply):
if reply in self.replyMap:
row = self.replyMap.get(reply)
del self.replyMap[reply]
pixmap = QPixmap()
pixmap.loadFromData(reply.readAll())
image = QLabel()
image.setPixmap(pixmap)
self.ui.tableWidget.setCellWidget(row, 0, image)
self.ui.tableWidget.setRowHeight(row, 160)
@pyqtSlot(QNetworkReply, list)
def on_sslErrors(self, reply, errors):
if reply in self.replyMap:
del self.replyMap[reply]