简体   繁体   中英

Using Chrome flags with QtWebEngine (PyQt5)

For the development of my PyQt5 browser project, I read here that by passing Chrome flags as application arguments, they will automatically be passed onto the QtWebEngineProcess.exe when it gets launched by the code. I have tried doing app = QApplication(sys.argv + [--enable-force-dark]) , but this doesn't make the chromium render the web pages in dark mode (I have also tries lots of variations of the flag name, so I assume this isn't the problem).

I am wondering whether it is possible to manually call the QtWebEngineProcess.exe with custom flags set, from the PyQt5 code by inheriting a class and overriding a function, and connecting the QtWebEngineView to this class, so like ...("QtWebEngineProcess.exe -[1st flag] -[2nd flag"]) ?

If the above method is not possible, is there any other way to use chromium with custom flags in the QtWebEngineView ? I am running PyQt5.14.2 (Chromium 77), Python 3.8.0, Windows 10

To set the chromium flags can be done using the following methods(See the docs ):

  • Pass as arguments to QApplication:

     args = ["--foo-arg=foo-value", "--bar-arg=bar-value"] app = QtWidgets.QApplication(args) # or # app = QtWidgets.QApplication(sys.argv + args)
  • Set it through the environment variable QTWEBENGINE_CHROMIUM_FLAGS :

     import os os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = "--foo-arg=foo-value --bar-arg=bar-value" app = QtWidgets.QApplication(sys.argv)

And therefore your attempt is correct but the problem seems to be that not all chromium flags are supported by Qt WebEngine and that seems to be the case for --enable-force-dark . Searching the net I found this post that provides an alternative: --blink-settings=darkMode=4,darkModeImagePolicy=2

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets

if __name__ == "__main__":
    import os
    import sys

    os.environ[
        "QTWEBENGINE_CHROMIUM_FLAGS"
    ] = "--blink-settings=darkMode=4,darkModeImagePolicy=2"
    app = QtWidgets.QApplication(sys.argv)

    # or
    # args = sys.argv + ["--blink-settings=darkMode=4,darkModeImagePolicy=2"]
    # app = QtWidgets.QApplication(sys.argv + args)

    view = QtWebEngineWidgets.QWebEngineView()
    view.load(QtCore.QUrl("https://www.google.com"))
    view.show()
    sys.exit(app.exec_())

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM