问题
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