5

how can i set Qicon from a url in PYQT , can you give me an example?

Anwar Mohamed
  • 625
  • 2
  • 13
  • 27
  • Take a look at [QWebSettings.iconForUrl (QUrl url)](http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwebsettings.html#iconForUrl) method. – RanRag May 24 '12 at 10:54
  • this will be fetch the website icon not icon file hosted on the site – Anwar Mohamed May 24 '12 at 11:11
  • 2
    Do you mean: receive the file *hosted at the URL* and use this as the icon? And *not* the "favicon" of the website which is typically used as the tab icon and in favorites menu in browsers? – leemes May 24 '12 at 11:16

2 Answers2

8

a basic example would be:

from PyQt4.QtGui import *
from PyQt4.QtCore import QUrl
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest

app = QApplication([])
url = "http://www.google.com/favicon.ico"
lbl = QLabel("loading...")
nam = QNetworkAccessManager()

def finishRequest(reply):
    img = QImage()
    img.loadFromData(reply.readAll())
    lbl.setPixmap(QPixmap(img))

nam.finished.connect(finishRequest)
nam.get(QNetworkRequest(QUrl(url)))
lbl.show()
app.exec_()
mata
  • 67,110
  • 10
  • 163
  • 162
1

use requests.get method to download the image and create a QIcon from it.

import sys
import requests

import PySide6
from PySide6.QtWidgets import QTableView, QWidget, QApplication, QGridLayout, QHeaderView
from PySide6.QtCore import Qt, QAbstractTableModel
from PySide6.QtGui import QColor, QIcon, QPixmap

from datetime import datetime

class MagicIcon():
    def __init__(self, link):
        self.link = link
        self.icon = QIcon()
        try:
            response = requests.get(self.link)
            pixmap = QPixmap()
            pixmap.loadFromData(response.content)
            self.icon = QIcon(pixmap)
        except:
            pass

class MainWindow(QWidget):
    def __init__():
        super().__init__()
        self.setWindowIcon(MagicIcon(
            "https://img.icons8.com/external-flatarticons-blue-flatarticons/65/000000/external-analysis-digital-marketing-flatarticons-blue-flatarticons-1.png"
        ).icon)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    wid = MainWindow()
    wid.show()
    sys.exit(app.exec())
Udesh
  • 2,415
  • 2
  • 22
  • 32