Æmmy
Æmmy

Reputation: 33

PyQt5 QWebEngineView Not Displaying Web Pages correctly

I'm attempting to create a simple web browser using PyQt5 on Termux Kali Linux rootless, and I've encountered an issue where the QWebEngineView doesn't display web pages as expected. I've followed the setup guidelines and ensured that all necessary components are imported and initialized. I test run the script useing a testlab

PyQt5

enter image description here

Expected:

enter image description here

Python3 Script:


import os
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtPrintSupport import *


class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.tabs = QTabWidget()
        self.tabs.setDocumentMode(True)
        self.tabs.tabBarDoubleClicked.connect(self.tab_open_doubleclick)
        self.tabs.currentChanged.connect(self.current_tab_changed)
        self.tabs.setTabsClosable(True)
        self.tabs.tabCloseRequested.connect(self.close_current_tab)
        self.setCentralWidget(self.tabs)
        self.status = QStatusBar()
        self.setStatusBar(self.status)
        navtb = QToolBar("Navigation")
        self.addToolBar(navtb)
        self.add_navigation_actions(navtb)
        self.urlbar = QLineEdit()
        self.urlbar.returnPressed.connect(self.navigate_to_url)
        navtb.addWidget(self.urlbar)
        self.add_new_tab(QUrl('http://www.google.com'), 'Homepage')
        self.show()
        self.setWindowTitle("Geek PyQt5")

    def add_navigation_actions(self, navtb):
        back_btn = QAction("Back", self)
        back_btn.setStatusTip("Back to previous page")
        back_btn.triggered.connect(lambda: self.tabs.currentWidget().back())
        navtb.addAction(back_btn)
        next_btn = QAction("Forward", self)
        next_btn.setStatusTip("Forward to next page")
        next_btn.triggered.connect(lambda: self.tabs.currentWidget().forward())
        navtb.addAction(next_btn)
        reload_btn = QAction("Reload", self)
        reload_btn.setStatusTip("Reload page")
        reload_btn.triggered.connect(lambda: self.tabs.currentWidget().reload())
        navtb.addAction(reload_btn)
        home_btn = QAction("Home", self)
        home_btn.setStatusTip("Go home")
        home_btn.triggered.connect(self.navigate_home)
        navtb.addAction(home_btn)
        navtb.addSeparator()
        stop_btn = QAction("Stop", self)
        stop_btn.setStatusTip("Stop loading current page")
        stop_btn.triggered.connect(lambda: self.tabs.currentWidget().stop())
        navtb.addAction(stop_btn)

    def add_new_tab(self, qurl=None, label="Blank"):
        if qurl is None:
            qurl = QUrl('http://www.google.com')

        browser = QWebEngineView()
        browser.setUrl(qurl)
        i = self.tabs.addTab(browser, label)
        self.tabs.setCurrentIndex(i)
        browser.urlChanged.connect(lambda qurl, browser=browser: self.update_urlbar(qurl, browser))
        browser.loadFinished.connect(lambda _, i=i, browser=browser: self.tabs.setTabText(i, browser.page().title()))

    def tab_open_doubleclick(self, i):
        if i == -1:
            self.add_new_tab()

    def current_tab_changed(self, i):
        qurl = self.tabs.currentWidget().url()
        self.update_urlbar(qurl, self.tabs.currentWidget())
        self.update_title(self.tabs.currentWidget())

    def close_current_tab(self, i):
        if self.tabs.count() < 2:
           return

        self.tabs.removeTab(i)

    def update_title(self, browser):
        if browser != self.tabs.currentWidget():
           return

        title = self.tabs.currentWidget().page().title()
        self.setWindowTitle("%s - Geek PyQt5" % title)

    def navigate_home(self):
        self.tabs.currentWidget().setUrl(QUrl("http://www.google.com"))

    def navigate_to_url(self):
        q = QUrl(self.urlbar.text())
        if q.scheme() == "":
           q.setScheme("http")

        self.tabs.currentWidget().setUrl(q)

    def update_urlbar(self, q, browser=None):
        if browser != self.tabs.currentWidget():
           return

        self.urlbar.setText(q.toString())
        self.urlbar.setCursorPosition(0)

app = QApplication(sys.argv)
app.setApplicationName("Geek PyQt5")
window = MainWindow()
app.exec_()

Terminal output:

┌──(kali㉿localhost)-[~]
└─$ python3 PyQt5_brower.py
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-kali'
[26203:26242:0616/105735.415438:ERROR:file_path_watcher_linux.cc(71)] Failed to read /proc/sys/fs/inotify/max_user_watches
[26203:26241:0616/105735.477987:ERROR:udev_watcher.cc(51)] Failed to initialize a udev monitor.
[26262:26269:0616/105736.390721:ERROR:command_buffer_proxy_impl.cc(141)] ContextResult::kTransientFailure: Failed to send GpuChannelMsg_CreateCommandBuffer.

Issue: When I run the code, the application launches and when I try to access a website it doesn't load the web page correctly as shown above.

What I've tried:

Ensured all necessary PyQt5 modules (PyQt5, PyQtWebEngine) are installed.

Checked the URL handling and tab management code, but couldn't identify any issues.

Question:

What could be causing the QWebEngineView to not display web pages correctly? How can I resolve this issue to ensure that web pages are loaded and displayed correctly?

Upvotes: 0

Views: 124

Answers (0)

Related Questions