如何通过webEngineContextLog禁用QWebEngineView的日志输出?
禁用PyQt6中QWebEngineView的webenginecontext日志输出
要禁用PyQt6中QWebEngineView输出的qt.webenginecontext信息日志,有两种常用方法:
方法一:通过QLoggingCategory设置日志等级
利用Qt的日志分类机制,将qt.webenginecontext的日志级别调整为Warning及以上,屏蔽Info级别的初始化日志。修改代码如下:
import sys from PyQt6.QtWidgets import QApplication from PyQt6.QtWebEngineWidgets import QWebEngineView from PyQt6.QtCore import QLoggingCategory # 设置日志过滤规则,关闭qt.webenginecontext的Info日志 QLoggingCategory.setFilterRules("qt.webenginecontext.info=false") app = QApplication(sys.argv) view = QWebEngineView() view.settings() sys.exit(app.exec())
方法二:通过环境变量控制
通过设置QT_LOGGING_RULES环境变量过滤指定日志,有两种实现方式:
方式1:在Python代码中设置
import os os.environ["QT_LOGGING_RULES"] = "qt.webenginecontext.info=false" from PyQt6.QtWidgets import QApplication from PyQt6.QtWebEngineWidgets import QWebEngineView app = QApplication(['test']) QWebEngineView().settings()
方式2:在终端/系统环境变量中设置
- Windows终端:
set QT_LOGGING_RULES=qt.webenginecontext.info=false
- Linux/macOS终端:
export QT_LOGGING_RULES=qt.webenginecontext.info=false
原理说明
你提到的webEngineContextLog.isInfoEnabled()控制着Info级日志的输出权限,上述两种方法本质都是将该分类的Info日志输出权限设为false,从而阻止这些初始化信息的打印。
内容的提问来源于stack exchange,提问作者dukeeloo
相关产品推荐
相关产品推荐

