Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, I'm am making a browser(like Firefox, Chrome, Opera) using pyqt5, the user can open new tabs & close them. I am trying to make the tab icon change to the websites favicon.

I did some research and I found out that most website store their icon in https://example.com/favicon.ico (their must be some better way to get the favicon from a website)

I am not able to find a method to display an image on the tab from the web.

What I have tried:

I tried this but then found out I cannot use QIcon to display the icon from the link. It only reads an image from a file path:
Python
current_tab_index = self.tabs.currentIndex()
url = "http://www.google.com/favicon.ico"
NetworkAccessManager = QNetworkAccessManager()

print(current_tab_index)
self.tabs.setTabIcon(current_tab_index, QIcon(url))

I found something with QPixmap, but do not know how to apply it to a tab:
Python
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtPrintSupport import *

from PyQt5 import QtCore, QtGui, QtWidgets

from PyQt5.QtCore import QUrl
from PyQt5.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_()
Posted
Updated 19-Feb-22 5:00am
v2

To do this you just put in the add tab function this:
self.tabs.setTabIcon(i, browser.icon())
browser.iconChanged.connect(lambda icon, browser=browser: self.update_icon(browser, icon))

and create this fuction:
def update_icon(self, browser, icon):
    index = self.tabs.indexOf(browser)
    self.tabs.setTabIcon(index, icon)

If you have any questions tell me

You can also see how I did it in my project here:

Just to know I also tried to find it for two months until today that I found this code that includes the solution
 
Share this answer
 
That's impossible. I have not seen anyway in the docs for that to work.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900