PyQt4 - setWindowIcon from external website

a 夏天 提交于 2019-12-11 19:35:11

问题


Im currently developing my first application in PyQt4, and what i would like to do now is to set my application's window icon from an image source on a website.

I thought something like this would work, but that is not the case: self.

# Sets the application icon
def set_app_icon(self, icon_url):
    self.setWindowIcon(QIcon(QUrl(icon_url)))

self.set_app_icon('http://web.page/icon.png')

How can i achieve this? Thank you in advance.


回答1:


QIcon cannot go and fetch data from web. You need to download it yourself and give it to QIcon. Something like this:

import sys
from PyQt4 import QtGui, QtCore, QtNetwork

class Main(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)

    def loadIconFromUrl(self, url):
        manager = QtNetwork.QNetworkAccessManager(self)
        manager.finished.connect(self._setIconFromReply)
        manager.get(QtNetwork.QNetworkRequest(QtCore.QUrl(url)))

    def _setIconFromReply(self, reply):
        p = QtGui.QPixmap()
        p.loadFromData(reply.readAll(), format="ico")
        self.setWindowIcon(QtGui.QIcon(p))

app = QtGui.QApplication(sys.argv)
main = Main()
main.loadIconFromUrl("http://en.wikipedia.org/favicon.ico")
main.show()
sys.exit(app.exec_())


来源:https://stackoverflow.com/questions/10105113/pyqt4-setwindowicon-from-external-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!