0

The part where webpage should be rendered gets white for a fraction of second and then gets empty enter image description here

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.

john.d
  • 31
  • 5
  • Are you using PyQt modules installed with pip or those provided by Ubuntu? Do you see any message in the terminal (not the IDE debug console)? – musicamante May 23 '22 at 11:48
  • 2
    Possible duplicate of e.g. https://stackoverflow.com/q/72131093/984421 (currently with a bounty) and https://stackoverflow.com/q/70988870/984421. Seems to be caused some kind of system-specific bug. – ekhumoro May 23 '22 at 12:06
  • Yes, same issue as linked question. I also have this issue in Ubuntu 22.04 but not in Ubuntu 21.10. Probable a missmatch between PyQtWebEngine 5.15.5 (installed via pip) and libqt5webengine5 5.19.9 (system library) – Carlos May 23 '22 at 17:32
  • I tested your code with PyQtWebEngine installed via PIP and didn't work, but also with PyQtWebEngine installed via system package (Ubuntu 22.04: `sudo apt install python3-pyqt5.qtwebengine`) and worked as expected. So probably an issue with the package available via PIP. – Carlos May 23 '22 at 17:45

5 Answers5

3

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.

Alexander
  • 16,091
  • 5
  • 13
  • 29
  • Thank you for answer, but I got the same result as with my code. – john.d May 23 '22 at 10:51
  • @john.d thats odd. It works on my machine – Alexander May 23 '22 at 10:52
  • What OS, python version and PyQt version do you have? – john.d May 23 '22 at 10:58
  • @john.d See if it works with the edit i just made... Im using windows – Alexander May 23 '22 at 10:59
  • Nope, even simple webpage is not rendered - result is same :-/ . – john.d May 23 '22 at 11:00
  • @john.d Check out edited answer – Alexander May 23 '22 at 11:39
  • @alexpdev if the package is available and installable, then it's not an issue of dropped support (the browser engine is *embedded* in the QtWebEngine). Besides, Qt5 is still supported, luckily. The issue is probably in the way the package was built, or might be related to graphics rendering. – musicamante May 23 '22 at 11:49
  • @musicamante Aren't those packages the same ones available for Ubuntu 21.10 and 20.04 or does apt literally have seperate packages for every version of ubuntu and all the other os's that support it? – Alexander May 23 '22 at 12:56
  • @alexpdev theoretically they are (but there could be some exceptions), but the issue might still affect *different* versions of the OS depending on the system configuration and libraries. For instance, the web engine heavily relies on the graphics support (painting is not done internally by the Qt drawing engine, but by the web engine renderer), so there could be issues related to graphics drivers, or the window manager (IIRC I've seen some reports related to Wayland). – musicamante May 23 '22 at 13:19
0

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
Carlos
  • 95
  • 9
  • Hello, Thanks for answer. Do you have any idea how to force virtualenv to use that library? I get "ModuleNotFoundError: No module named 'PyQt5.QtWebEngineWidgets' " error when I uninstalled pip package (and installed one through apt). Also could you please check what version is this python3-pyqt5.qtwebengine for example through command "dpkg -l | grep python3-pyqt5.qtwebengine" ? Mine seems to be 5.15.5 - same as version installed by pip, so I am not sure, if this would work at all. – john.d May 24 '22 at 14:54
  • OK, I managet to use installation library from apt outside virtualenv, but i got "Could not find QtWebEngineProcess" error when tried to execute script. Sigh. – john.d May 24 '22 at 15:15
  • @john.d see this related answer: https://stackoverflow.com/a/72422626/1378811 – Carlos Jun 09 '22 at 21:54
0

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.

Willow Media
  • 41
  • 1
  • 5
0

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).

trinkner
  • 374
  • 4
  • 15
0

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:

  1. In Qt Designer, drag a QLabel or other widget onto a QFrame.

  2. Apply a Layout named 'webEngineLayout' to the QLabel.

  3. Delete the widget (yes, I know this seems odd, but do it anyway).

  4. 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.

  • Widgets can only have widgets as parents. Because of this, adding a widget to a layout automatically reparents it (since layouts aren't widgets). Assuming your propsed solution actually works: what specific difference (if any) does this make to the xml in the resulting ui file? Is there some property that gets set that wouldn't otherwise be? If so, this might be helpful in diagnosing the real source of the problem. It shouldn't be too difficult for you to investigate this by making a simple comparision, and it seems likely your answer could become much more useful if you did so. – ekhumoro Jun 26 '23 at 20:27