3

I'm trying to make a browser, using Python 3.10.4 & PyQt5 v5.15.6, in a virtual environment (venv).

My problem is that QWebEngineView doesn't load URL, so I just have a blank window.

Here is my code:

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.load(QUrl('https://www.google.com'))
        self.browser.loadFinished.connect(self.test)
        self.setCentralWidget(self.browser)
        self.showMaximized()

    def test(self):
        print('super')


app = QApplication(sys.argv)
QApplication.setApplicationName('Jello')
window = MainWindow()
app.exec_()

Do you have any idea of a solution for that? I've walked through many web pages without finding any lead.

Thanks!

Nimantha
  • 6,405
  • 6
  • 28
  • 69
jauche
  • 61
  • 8
  • Are you running this in a venv, a virtual machine or what? What version of Qt/Python/OS? – musicamante May 05 '22 at 17:44
  • Same issue here, I saw this problem first with a Manjaro update, and now in Ubuntu 22.04. However, in Ubuntu 21.10 it works. Also tried Python 3.9 and 3.10, same results. Details on my Ubuntu 22.04 environment, where the error exists: ``` PyQt5 5.15.6 PyQt5-Qt5 5.15.2 PyQtWebEngine 5.15.5 PyQtWebEngine-Qt5 5.15.2 ``` – Carlos May 18 '22 at 14:59
  • Also: libqt5webengine5 5.19.9, libqt5core5a 5.13.3 – Carlos May 18 '22 at 15:06
  • Working environment (Ubuntu 21.10): PyQt5 5.15.6, PyQt5-Qt5 5.15.2, PyQtWebEngine 5.15.5, PyQtWebEngine-Qt5 5.15.2, libqt5webengine5 5.15.6, libqt5core5a 5.12.2. So probably a mismatch between Python and System libraries? – Carlos May 18 '22 at 15:16
  • @Carlos The minor version number of PyQt doesn't always match the actual Qt version, what you should look for is the actual Qt version. Ensure that all main Qt and related modules are properly updated through your package manager (or pip, if you used it) and eventually try to uninstall and reinstall them again – musicamante May 22 '22 at 15:06
  • I tested with Ubuntu 22.04 system libraries and the code works. So the problem seems to be the PyQtWebEngine module from PIP. I executed: `sudo apt install python3-pyqt5.qtwebengine` – Carlos May 23 '22 at 17:39

2 Answers2

6

I had the same issue on Linux Manjaro KDE
the solution is to add '--no-sandbox' as a second item into QApplication() argument list like this:

app = QApplication(['', '--no-sandbox'])

so,
this code works fine:

from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5 import QtCore
app = QApplication(['', '--no-sandbox'])
main = QWebEngineView()
main.load(QtCore.QUrl(f"https://fast.com"))
main.show()
app.exec_()

enter image description here

but this one: does Not work:

from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5 import QtCore
app = QApplication([])
main = QWebEngineView()
main.load(QtCore.QUrl(f"https://fast.com"))
main.show()
app.exec_()

enter image description here

ibrahem
  • 370
  • 3
  • 12
  • I had the same error with showing a pdf in a window, spent a lot of time to find why. Adding '--no-sandbox' to app = QApplication(['', '--no-sandbox']) now allows showing the pdf in the proper window using debian (tested on ubuntu 22 and Mint). – sol Nov 14 '22 at 09:38
  • using pyqt5 15.5.7 and PyQtWebEngine 5.15.6 on python3.10.6 – sol Nov 14 '22 at 09:39
  • @sol, you can use [Mozilla's PDF.js](https://mozilla.github.io/pdf.js/) as a workaround to render PDF as an HTML page – ibrahem Nov 14 '22 at 22:41
  • 1
    --no-sandbox solved my issue on Ubuntu 22.04. I'm glad I found this! – user693336 Aug 20 '23 at 23:33
  • @user693336, you are welcome [: – ibrahem Aug 21 '23 at 11:20
0

You didn't mention your OS and version, but I've seen this error in Ubuntu 22.04. Try installing PyQtWebEngine via system package and running your code outside the virtual environment. In Ubuntu:

sudo apt install python3-pyqt5.qtwebengine
Carlos
  • 95
  • 9