QIcon
无法从网络上获取数据。您需要自己下载并提供给QIcon
. 像这样的东西:
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_())