0

I can't figure out how to make pages like react.dev, angular.dev, facebook.com, or chat.openai load properly.

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import *

# Define a class MyWebBrowser that inherits from QMainWindow
class MyWebBrowser(QMainWindow):
    def __init__(self, *args, **kwargs):
        # Call the constructor of the base class (QMainWindow)
        super(MyWebBrowser, self).__init__(*args, **kwargs)

        # Create the main window and set its title
        self.window = QWidget()

        # Create a horizontal layout for the main window
        self.layout = QHBoxLayout(self.window)
        self.layout.setContentsMargins(0, 0, 0, 0)

        # Create a QWebEngineView widget for displaying web content
        self.browser = QWebEngineView()
        self.layout.addWidget(self.browser)

        # Access the settings of the web engine
        self.settings = self.browser.settings()

        # Enable JavaScript in the web engine
        self.settings.setAttribute(QWebEngineSettings.JavascriptEnabled, True)

        # Set the initial URL to "https://react.dev"
        self.browser.setUrl(QUrl("https://react.dev"))

        # Define a mobile user agent string
        self.mobile_user_agent = (
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
        )

        # Set the user agent for the web page
        self.browser.page().profile().defaultProfile().setHttpUserAgent(self.mobile_user_agent)

        # Set the layout for the main window
        self.window.setLayout(self.layout)

        # Set the default size for the main window
        self.defaultSize = QSize(500, 800)
        self.window.setFixedSize(self.defaultSize)

        # Show the main window
        self.window.show()

# Create a QApplication instance
app = QApplication([])

# Create an instance of MyWebBrowser
window = MyWebBrowser()

# Start the application event loop
app.exec_()

I hope the mentioned pages load and function properly; for instance, the button to switch to the dark theme on React doesn't work, or the background of Angular doesn't display.

4
  • 2
    Please clarify what you mean by "load properly", since in your last paragraph you're mentioning a button switch, which is quite different from a loading issue. Also, provide proper environment information (such as OS and Qt - not PyQt - version). Finally, consider that Qt5 is now considered obsolete, and its QtWebEngine module uses a chromium engine that, for web standards, is potentially obsolete too. Commented Jan 6, 2024 at 5:10
  • My bad for not making myself clear. Some pages like React or Angular just don't load right. Take chat.openai.com, for instance – the login and register buttons are basically on vacation. Didn't realize it was ancient tech :,) Any other options you'd suggest I dive into? Commented Jan 6, 2024 at 5:15
  • If you want to show websites oriented on web-development, ignoring the browser version is an irresponsible mistake. A 2-3 year old browser is often obsolete, especially for such websites; if your program must access such sites, consider switching to Qt6. In case you do know specific requirements for a certain website you have to use in your program, check the minimum browser version requirement. For instance, the latest Qt5 (>=5.15.3) uses Chromium 87 (released on April 2021): the current one (Jan 5/2024) is 122. See QtWebEngine/ChromiumVersions. Commented Jan 6, 2024 at 5:42
  • It's still a little premature to consider Qt5 obsolete. The commercial LTS version will be supported until 26-5-2025. Also, many Linux distros are still backporting patches for the open source edition, and that's unlikely to change much until KDE 6 is out of beta and widely deployed. Beyond that, there will still be a lot of legacy KDE/Qt apps around that will need continued Qt5 support. Commented Jan 6, 2024 at 18:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.