QWebEngineView crashes when accessing specific websites - python

I have a very weird bug with QWebEngineView. Below is some code which creates a QWebEngineView.
import sys
from PyQt5.QtCore import QCoreApplication, QFileInfo, QUrl
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings, QWebEngineProfile, QWebEnginePage
class Widget(QWidget):
def __init__(self):
super().__init__()
webview = QWebEngineView(self)
webview.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
profile = QWebEngineProfile("my_profile", webview)
webpage = QWebEnginePage(profile, webview)
webview.setUrl(QUrl("https://www.youtube.com"))
#self.webview.setPage(self.webpage)
webview.setGeometry(0, 0, 800, 480)
webview.show()
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Widget()
app.exec_()
My issue is that when I run the code as it is (with the self.webview.setPage(self.webpage) line commented out) I am able to open PDF files, however opening YouTube crashes the program. However if I uncomment the line and run it, I can't open PDF files (although it doesn't crash the program it just doesn't open the PDF file), but I can then open YouTube with no issue.

You have to set the QWebEngineProfile and QWebEnginePage first and then enable the plugins:
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget
from PyQt5.QtWebEngineWidgets import (
QWebEnginePage,
QWebEngineProfile,
QWebEngineSettings,
QWebEngineView,
)
class Widget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
profile = QWebEngineProfile("my_profile", self)
webpage = QWebEnginePage(profile, self)
webview = QWebEngineView(self)
webview.setPage(webpage)
webview.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
webview.load(QUrl("https://www.youtube.com"))
lay = QVBoxLayout(self)
lay.addWidget(webview)
self.resize(640, 480)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Widget()
w.show()
app.exec_()

Related

Error "Parameter 'url' unfilled" using PyQt5 with url set

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.browser = QWebEngineView
self.browser.setUrl(QUrl("http://google.com"))
self.setCentralWidget(self.browser)
self.showMaximized()
app = QApplication(sys.argv)
QApplication.setApplicationName('Browser')
window = MainWindow
app.exec_()
This is so far my code, but it gives me an error at the Url line although i've set google.com
You forgot the parenthesis after MainWindow when calling the class at the end, as well as you need to add window.show().
You also need to specify to QWebEngineView an argument (in this case self).
So adding those things the code would look like this:
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.browser = QWebEngineView(self)
self.browser.setUrl(QUrl("http://google.com"))
self.setCentralWidget(self.browser)
self.showMaximized()
app = QApplication(sys.argv)
QApplication.setApplicationName('Browser')
window = MainWindow()
window.show()
app.exec_()
Tell me if it doesn't work for you.

Can I implement a random font selection?

I have a program that displays some message on a label (using QtDesigner):
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
class MyWidget(QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi('main1.ui', self)
self.run()
def run(self):
self.label.setText('Message')
app = QApplication(sys.argv)
ex = MyWidget()
ex.show()
sys.exit(app.exec_())
This message displays in the selected font in the QtDesigner, StyleSheet of my label:
The question is: What can I do to make this font be randomly selected? Is it possible? (Perfect case: every time i run my program it shows my message in some randomly selected font)
You can obtain all the available families through the families() method of QFontDatabase, choose one randomly, build a QFont and set it in the QLabel:
import random
import sys
from PyQt5 import uic
from PyQt5.QtGui import QFont, QFontDatabase
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
class MyWidget(QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi("main1.ui", self)
db = QFontDatabase()
family = random.choice(db.families())
print(family)
font = db.font(family, "", 72)
# also random style:
# style = random.choice(db.styles(family))
# font = db.font(family, style, 72)
self.label.setFont(font)
self.run()
def run(self):
self.label.setText("Message")
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = MyWidget()
ex.show()
sys.exit(app.exec_())

pyqt5 qwebenginview doesn't autoplay youtube videos

I am trying to use Qwebengineview to view a list of youtube videos but the browser doesn't autoplay the videos, I am using PyQt5 5.13.1 Python 3.6
here is a sample code
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)
webview = QWebEngineView()
profile = QWebEngineProfile("my_profile", webview)
profile.defaultProfile().setPersistentCookiesPolicy(QWebEngineProfile.ForcePersistentCookies)
webpage = QWebEnginePage(profile, webview)
webview.setPage(webpage)
webview.load(QUrl("https://www.youtube.com/watch?v=VzIVI2fsRb0"))
webview.show()
sys.exit(app.exec_())
I have find a solution for this using QWebEngineSettings and here is a full working example in case someone needs it
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage, QWebEngineSettings
from PyQt5.QtWidgets import QApplication
import time
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
webview = QWebEngineView()
profile = QWebEngineProfile("my_profile", webview)
profile.defaultProfile().setPersistentCookiesPolicy(QWebEngineProfile.ForcePersistentCookies)
webpage = QWebEnginePage(profile, webview)
webpage.settings().setAttribute(QWebEngineSettings.PlaybackRequiresUserGesture, False)
webview.setPage(webpage)
webview.load(QUrl("https://www.youtube.com/watch?v=aKCNrkERJ3E"))
webview.show()
sys.exit(app.exec_())

How do I change the MainWindow Title in Python when using Form()?

I am trying to change the title of the window applications but I don't see how to do it for my specific case where I am loading the *.ui file as a form.
A simplified version of my code so far looks like so:
import sys, os
from sys import stdout, stdin, stderr
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication, QPushButton, QLineEdit, QTextBrowser
from PySide2.QtCore import QFile, QObject, QEvent
class Form(QObject):
def __init__(self, ui_file, parent=None):
super(Form, self).__init__(parent)
ui_file = QFile(ui_file)
ui_file.open(QFile.ReadOnly)
loader = QUiLoader()
self.window = loader.load(ui_file)
ui_file.close()
self.window.show()
if __name__ == '__main__':
print("Starting up tool application...\nPlease wait.")
app = QApplication(sys.argv)
form = Form('mifareclassictool.ui')
sys.exit(app.exec_())
I tried self.setWindowTitle("title") within the Form class but that property does not exist. I am still new to Qt Designer and creating applications in python.
The form is not the window so you can not use self.setWindowTitle("title"), instead you should use self.window:
self.window.setWindowTitle("title")

PyQt5 5.9 , setWindowIcon( QIcon(*WEB_LINK*) )

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
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_())

Categories

Resources