1

Hi, I have an issue with my PyQt5 setWindowIcon.

When I try to set my window icon from a local image, it works perfectly. But when I try to put a online link like:

setWindowIcon( QIcon("https://www.google.ge/images/branding/product/ico/googleg_lodp.ico") )
it does not work. What to do? Its 32x32 ico btw.
~Thanks

Pixsa
  • 571
  • 6
  • 16
  • Are you serious? Just download it and save it to disk. – ekhumoro Jan 15 '18 at 01:15
  • Lol, I am building a dynamic thing... – Pixsa Jan 22 '18 at 21:52
  • @ვანიჩკაა the url that Qt uses only refers to local files or files that are in a .qrc, if you want to use an url icon you should download it anyway, if this changes you should download it and update it again. If you want to know how to download it with PyQt read the following: https://stackoverflow.com/questions/10735977/setting-qicon-pixmap-from-url – eyllanesc Jan 22 '18 at 22:06
  • @ვანიჩკაა. That makes no difference. One way or another, the image file will have to be downloaded. How else do you imagine it could work? – ekhumoro Jan 22 '18 at 22:13
  • @ekhumoro thank you, but I noticed that it applies to a second, and then disappears, do you have any idea whats going on here? – Pixsa Jan 23 '18 at 03:06
  • @ვანიჩკაა. Apply what? Apply how? I have no idea what you're trying to do now. – ekhumoro Jan 23 '18 at 04:29
  • The icon is working for second and then disappears when using following code in this question. – Pixsa Jan 23 '18 at 11:29

1 Answers1

2

You have to use QNetworkAccessManager and manually download image from url. Then read bytes from response, create a QPixmap (beacuse it has loadFromData method) and initialize a QIcon from QPixmap.

And after that you will be able to set window icon.

import sys

from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout

ICON_IMAGE_URL = "https://www.google.ge/images/branding/product/ico/googleg_lodp.ico"


class MainWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        self.label = QLabel('Image loading demo')

        self.vertical_layout = QVBoxLayout()
        self.vertical_layout.addWidget(self.label)

        self.setLayout(self.vertical_layout)

        self.nam = QNetworkAccessManager()
        self.nam.finished.connect(self.set_window_icon_from_response)
        self.nam.get(QNetworkRequest(QUrl(ICON_IMAGE_URL)))

    def set_window_icon_from_response(self, http_response):
        pixmap = QPixmap()
        pixmap.loadFromData(http_response.readAll())
        icon = QIcon(pixmap)
        self.setWindowIcon(icon)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())