Reputation: 31
The part where webpage should be rendered gets white for a fraction of second and then gets empty
Here is my code (basically it is https://www.pythonguis.com/examples/python-web-browser/):
from PyQt5.QtCore import QUrl
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 = QWebEngineView()
self.browser.setUrl(QUrl("https://www.google.com"))
self.setCentralWidget(self.browser)
self.show()
app = QApplication(sys.argv)
window = MainWindow()
app.exec_()
Here is similar code, which I use for rendering html in from my local folder (also does not work - same symptoms):
from PyQt5.QtCore import QUrl
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 = QWebEngineView()
file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'temporary_files', "map.html"))
self.browser.load(QUrl.fromLocalFile(file_path))
self.setCentralWidget(self.browser)
self.show()
app = QApplication(sys.argv)
window = MainWindow()
app.exec_()
PyQt5.15.6, python3.8, OS Ubuntu 22.04 LTS. It worked before on ubuntu 18.04, problems started after reinstalling system, although I backed up and restored virtual environment, so libraries should be the same.
Upvotes: 0
Views: 4191
Reputation: 35
I found using os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = "--no-sandbox"
fixed the problem in my case!
Upvotes: 1
Reputation: 3
In my case, this wasn't a Ubuntu problem. Instead, it arises when using Python PyQt5 and Qt Designer, and manually applying the engine as a child to a parent widget such as QFrame. Apparently the engine was working fine, but PyQt5 wasn't displaying QWebEngineView's output or any indication of a problem.
I found that the engine likes its parent to be a Layout, but doesn't like a QFrame!
Others may have a much better method, but I did this:
In Qt Designer, drag a QLabel or other widget onto a QFrame.
Apply a Layout named 'webEngineLayout' to the QLabel.
Delete the widget (yes, I know this seems odd, but do it anyway).
In your Python program or its equivalent enter this:
self.webEngineView = QWebEngineView()
self.webEngineLayout.addWidget(self.webEngineView)
To prove that this works, add Alexander's test to the above:
self.webEngineView.setUrl(QUrl("https://www.google.com"))
A much better solution would be to create a Qt Designer widget for QWebEngineView.
This is my first posting, so my here's my apologies for the wordiness etc, but it's taken endless days to find this solution.
Upvotes: 0
Reputation: 384
I solved this same issue on a fresh install of Ubuntu 22.04 LTS.
I think I had created the problem by using pip to install pyqt5, pyqt5-sip, and pywebengine.
I uninstalled all three of these with pip.
Then I ran my python script and it used the system's default pyqt5 which was already installed.
If you find that pyqt5 was not already installed, try installing it via apt-get instead of via pip.
I suspect that the problem was that I mistakenly had two copies of pyqt5 installed and the one installed with pip was conflicting with the system default one (which seems to be controlled by apt-get).
Upvotes: 1
Reputation: 41
Might be a bit late to reply, but I just did a fresh install of Ubuntu server 22.04 and tried a script with the QWebEngineView. And I have the same issue. After removing .cache and .local from the users home folder, which includes the pip installed files, my application is working. It's indeed an issue between pip and the ubuntu provided files. So, skip using pip on this Ubuntu release was my solution.
Upvotes: 0
Reputation: 95
I'm seeing the same issue in Ubuntu 22.04 but not in Ubuntu 21.10 when installing PyQtWebEngine via PIP. Try installing PyQtWebEngine via system package and running your code outside the virtual environment. In Ubuntu:
sudo apt install python3-pyqt5.qtwebengine
Upvotes: 0
Reputation: 17281
Try this:
from PyQt6.QtCore import QUrl
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
from PyQt6.QtWebEngineWidgets import *
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow,self).__init__(*args, **kwargs)
self.browser = QWebEngineView()
self.setCentralWidget(self.browser)
# self.browser.setUrl(QUrl("https://www.google.com"))
self.browser.setHtml("<html><body><h1>Hello World... Hello World</h1></body></html>")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show() # moved show outside main widget
sys.exit(app.exec()) # use app.exec instead of app.exec_
Try using PyQt6 instead of PyQt5. There are issues with Qt5-WebEngineWidgets on Ubuntu 22.04.
Upvotes: 3