在 QtWebEngine (PyQt5) 中使用 Chrome 标志 [英] Using Chrome flags with QtWebEngine (PyQt5)

查看:170
本文介绍了在 QtWebEngine (PyQt5) 中使用 Chrome 标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的 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_())

这篇关于在 QtWebEngine (PyQt5) 中使用 Chrome 标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆