How to set a QWebEngineProfile to a QWebEngineView - python

I want to set different QWebEngineProfiles to different QWebEngineViews meaning each view will have its own cookie store. I cant find any documentation on it therefore all help would be greatly appreciated.
Any suggestion of another method of setting independent cookie stores to independent webviews will also help. Cheers.
Code is below (connecting the signal did not format properly here but rest assured it is correct in the real code):
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args,**kwargs)
self.browser={}
self.cookiestore={}
self.page={}
No = input("No: ")
for i in range(int(No)):
self.browser[str(i)] = QWebEngineView()
storagename = str(i)
self.cookiestore[str(i)] = QWebEngineProfile(storagename, self.browser[str(i)])
self.page[str(i)] = QWebEnginePage(self.cookiestore[str(i)], self.browser[str(i)])
self.browser[str(i)].setPage(self.page[str(i)])
self.browser[str(i)].load(QUrl("https://www.google.com"))
self.browser[str(i)].loadFinished.connect(lambda:self._loaded(str(i)))
def _loaded(self, No):
self.browser[No].page().toHtml(self._callable)
def _callable(self, data):
self.html = data
if "" in self.html:
print("Done")
else:
print("wait")
app = QApplication(sys.argv)
window = MainWindow()
app.exec_()

If you want to establish a QWebEngineProfile to a QWebEngineView you must do it through a QWebEnginePage as I show below:
webview = QWebEngineView()
profile = QWebEngineProfile("somestorage", webview)
webpage = QWebEnginePage(profile, webview)
webview.setPage(webpage)
Example:
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage
from PyQt5.QtWidgets import QApplication
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
views = []
for i in range(5):
webview = QWebEngineView()
profile = QWebEngineProfile(f"storage-{i}", webview)
webpage = QWebEnginePage(profile, webview)
webview.setPage(webpage)
webview.load(QUrl("https://stackoverflow.com/questions/48142341/how-to-set-a-qwebengineprofile-to-a-qwebengineview"))
webview.show()
views.append(webview)
sys.exit(app.exec_())

Related

Get non url-link when clicking on Link in PyQt5 QWebEngineView

I want to open self written html code in PyQt5 QWebEngineView. The html code should contain links, which are not url pages but have other information. Below a created a simple example. When clicking on the link named Link, python does not return the parameter inside the link. When I change the value of the link to a url, I get the QUrl as a result. Is there a way to get the content of the link (not a url) when clicked?
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
import sys
class MyWebEnginePage(QWebEnginePage):
def acceptNavigationRequest(self, url, _type, isMainFrame):
if _type == QWebEnginePage.NavigationTypeLinkClicked:
print(url)
return False
return super().acceptNavigationRequest(url, _type, isMainFrame)
class App(QMainWindow):
def __init__(self):
super(App, self).__init__()
browser = QWebEngineView()
browser.setPage(MyWebEnginePage(self))
link = "Hallo Welt"
text = f'<a style="text-decoration:none;" href="{link}">Link</a>'
browser.setHtml(text)
self.setCentralWidget(browser)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = App()
window.setGeometry(800, 100, 1000, 800)
window.show()
sys.exit(app.exec_())
You can use a url with the data: scheme to send the link value. When the url is received, you can then check that it has the correct scheme, and emit a signal with only the url's path (which does not include the scheme).
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
import sys
class MyWebEnginePage(QWebEnginePage):
dataLinkClicked = pyqtSignal(str)
def acceptNavigationRequest(self, url, _type, isMainFrame):
if (_type == QWebEnginePage.NavigationTypeLinkClicked and
url.scheme() == 'data'):
# send only the url path
self.dataLinkClicked.emit(url.path())
return False
return super().acceptNavigationRequest(url, _type, isMainFrame)
class App(QMainWindow):
def __init__(self):
super(App, self).__init__()
browser = QWebEngineView()
page = MyWebEnginePage(self)
# connect to the signal
page.dataLinkClicked.connect(self.handleDataLink)
browser.setPage(page)
# use a data-url
link = "data:Hallo Welt"
text = f'<a style="text-decoration:none;" href="{link}">Link</a>'
browser.setHtml(text)
self.setCentralWidget(browser)
def handleDataLink(self, text):
print(text) # prints "Hallo Welt"
if __name__ == '__main__':
app = QApplication(sys.argv)
window = App()
window.setGeometry(800, 100, 1000, 800)
window.show()
sys.exit(app.exec_())

How get icon from url and set to windowIcon

When I try to set my icon from url which contain favicon.ico it work fine, but when I try to set my icon from url which contain **.jpg or .png ** format it does not work and return none. Kindly help me to solve the problem.
you can check the following code.
# url = "http://www.google.com/favicon.icon
url = 'http://www.geo.tv/assets/front/images/gn-icons/256x256.jpg'
self.nam.finished.connect(self.finishRequest)
self.nam.get(QNetworkRequest(QUrl(url)))
def finishRequest(self,reply):
img = QImage()
img.loadFromData(reply.readAll())
self.setWindowIcon(QIcon(img))
I want to get url of image(png,jpg) mean url of icon of a website. and set to a label, windowIcon etc.
but it works only with image(.ico) format and does not work with other format and return nothing.
Analyzing the url I have seen that redirection from http to https so the reply does not have the bytes of the image but the following url. One possible solution is to have Qt handle redirects using request.setAttribute(QNetworkRequest.FollowRedirectsAttribute, True).
import sys
from functools import cached_property
from PySide2.QtCore import Signal, QObject, Qt, QUrl
from PySide2.QtGui import QImage, QPixmap, QIcon
from PySide2.QtNetwork import QNetworkAccessManager, QNetworkReply, QNetworkRequest
from PySide2.QtWidgets import QApplication, QWidget
class ImageDownloader(QObject):
finished = Signal(QImage)
def __init__(self, parent=None):
super().__init__(parent)
self.manager.finished.connect(self.handle_finished)
#cached_property
def manager(self):
return QNetworkAccessManager()
def start_download(self, url):
request = QNetworkRequest(url)
request.setAttribute(QNetworkRequest.FollowRedirectsAttribute, True)
self.manager.get(request)
def handle_finished(self, reply):
if reply.error() != QNetworkReply.NoError:
print("error: ", reply.errorString())
return
image = QImage()
ok = image.loadFromData(reply.readAll())
if not ok:
print("error")
return
self.finished.emit(image)
class Widget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.downloader = ImageDownloader()
self.downloader.finished.connect(self.handle_finished)
url = QUrl("http://www.geo.tv/assets/front/images/gn-icons/256x256.jpg")
self.downloader.start_download(url)
self.resize(640, 480)
def handle_finished(self, image):
pixmap = QPixmap.fromImage(image)
self.setWindowIcon(QIcon(pixmap))
def main():
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()

How do I update my PyQt5 icon every 60 seconds?

I am new to python and I am makeing a simple appliacation where the value of bitcoin is displayed in the system tray. I am useing PyQt5.
My question is: How do I refresh the value and icon every minute. When I try it my menu wont work anymore. Is there a simple fix?
This is my code:
import sys
import time
import math
import json
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QMenu
from PyQt5.QtGui import QIcon
from urllib.request import urlopen
from time import sleep
app = QApplication(sys.argv)
while True:
# Connecting the api
with urlopen("https://api.alternative.me/v2/ticker/1/") as response:
source = response.read()
data = json.loads(source)
price = (json.dumps(data['data']['1']['quotes']['USD']['price']))
change = (json.dumps(data['data']['1']['quotes']['USD']['percentage_change_1h']))
intPrice = float(price)
intChange = float(change)
roundPrice = round(intPrice,2)
roundStringPrice = str(roundPrice)
# Icon change
if intChange <=0.00:
trayIcon = QSystemTrayIcon(QIcon('icons/down.png'), parent=app)
else:
trayIcon = QSystemTrayIcon(QIcon('icons/up.png'), parent=app)
trayIcon.setToolTip(roundStringPrice + ' USD')
trayIcon.show()
time.sleep(5)
trayIcon.update()
menu = QMenu()
exitAction = menu.addAction('Quit app')
exitAction.triggered.connect(app.quit)
trayIcon.setContextMenu(menu)
sys.exit(app.exec_())
Don't use while True, urlopen or time.sleep as they block the eventloop, instead use a QTimer with QNetworkAccessManager:
import os.path
import json
from functools import cached_property
from PyQt5.QtCore import pyqtSignal, QObject, QTimer, QUrl
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkReply, QNetworkRequest
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMenu, QMessageBox, QSystemTrayIcon
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
class ApiManager(QObject):
infoChanged = pyqtSignal(float, float)
def __init__(self, parent=None):
super().__init__(parent)
self.manager.finished.connect(self.handle_finished)
#cached_property
def manager(self):
return QNetworkAccessManager()
def start_request(self):
url = QUrl("https://api.alternative.me/v2/ticker/1/")
qrequest = QNetworkRequest(url)
self.manager.get(qrequest)
def handle_finished(self, reply):
if reply.error() != QNetworkReply.NoError:
print(f"code: {reply.error()} message: {reply.errorString()}")
else:
print("successful")
source = reply.readAll().data()
data = json.loads(source)
r = data["data"]["1"]["quotes"]["USD"]
price = r["price"]
change = r["percentage_change_1h"]
self.infoChanged.emit(price, change)
class SystemTrayIcon(QSystemTrayIcon):
def __init__(self, parent=None):
super().__init__(parent)
self.setIcon(QIcon(self.get_resource("icons/up.png")))
self.menu = QMenu()
exitAction = self.menu.addAction("Quit app")
exitAction.triggered.connect(QApplication.quit)
self.setContextMenu(self.menu)
self.manager = ApiManager()
self.manager.infoChanged.connect(self.handle_info_changed)
timer = QTimer(self, timeout=self.manager.start_request, interval=60 * 1000)
timer.start()
self.manager.start_request()
def get_resource(self, path):
return os.path.join(CURRENT_DIR, path)
def handle_info_changed(self, price, change):
icon = (
QIcon(self.get_resource("icons/down.png"))
if change < 0
else QIcon(self.get_resource("icons/up.png"))
)
self.setIcon(icon)
self.setToolTip(f"{price:.2f} USD")
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
if not QSystemTrayIcon.isSystemTrayAvailable():
QMessageBox.critical(
None, "Systray", "I couldn't detect any system tray on this system."
)
sys.exit(1)
QApplication.setQuitOnLastWindowClosed(False)
trayIcon = SystemTrayIcon(parent=app)
trayIcon.show()
sys.exit(app.exec_())

Setting QIcon() pixmap from URL

how can i set Qicon from a url in PYQT , can you give me an example?
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_()
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())

QtWebkit: loadFinished and loadProgress slots are never executed

Here is a basic PyQt code for Webkit I was trying out.
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *
class XPrinter(QObject):
def __init__(self):
QObject.__init__(self)
def print_page_info(self, ok):
print ok
def print_load_started(self):
print 'started loading'
def print_load_percent(self, percent):
print percent
app = QApplication(sys.argv)
web = QWebView()
xprinter = XPrinter()
QObject.connect(web, SIGNAL("loadFinished()"), xprinter.print_page_info)
QObject.connect(web, SIGNAL("loadStarted()"), xprinter.print_load_started)
QObject.connect(web, SIGNAL("loadProgress()"), xprinter.print_load_percent)
web.load(QUrl("http://www.gnu.org"))
web.setWindowState(Qt.WindowMaximized)
web.show()
sys.exit(app.exec_())
I am facing the problem that the slots loadFinished and loadProgress are never executed. Please tell me where I am doing it wrong?
Try using the new style signals
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *
class XPrinter(QObject):
def __init__(self):
QObject.__init__(self)
def print_page_info(self, ok):
print ok
def print_load_started(self):
print 'started loading'
def print_load_percent(self, percent):
print percent
app = QApplication(sys.argv)
web = QWebView()
xprinter = XPrinter()
web.loadFinished.connect(xprinter.print_page_info)
web.loadStarted.connect(xprinter.print_load_started)
web.loadProgress.connect(xprinter.print_load_percent)
web.load(QUrl("http://www.gnu.org"))
web.setWindowState(Qt.WindowMaximized)
web.show()
sys.exit(app.exec_())
EDIT:
Also you had the wrong signatures
QWebview
QObject.connect(web, SIGNAL("loadFinished(bool)"), xprinter.print_page_info)
QObject.connect(web, SIGNAL("loadStarted()"), xprinter.print_load_started)
QObject.connect(web, SIGNAL("loadProgress(int )"), xprinter.print_load_percent)

Categories