How to suppress js error output in QtWebEngine?

When you debug a QtWebEngine application, you may use qDebug to print information here and there. You need to turn on the console by adding a line “CONFIG += console” in your .pro file. Then your program comes with a dos window where the debug information is displayed.

This is fine if your program is not a qtwebengine program. If you create a QWebEngineView object to load urls, you may find your console window is flooded with massive messages from QWebEngineView. These messages are not generated by you using qDebug, but QWebEngineView itself. These messages are produced  by javascript running in QWebEngineView, such as “js: Uncaught TypeError: Cannot set property ‘innerHTML’ of null”. These error messages are typically caused by errors on the webpage you are loading in the QWebEngineView  object, not your program. When javascript calls console.log() to output a warning or a fatal message, it will be displayed on your console window.

How to disable the output of javascript? To remove the javascript log information from your console, you need to re-implement the  javaScriptConsoleMessage function of QWebEnginePage in your subclass, then set the webenginepage object to the QWebEngineView object. You can do nothing in the re-implementation of the  javaScriptConsoleMessage function, i.e., an empty function body, then there would be no output to the console. The default implementation of javaScriptConsoleMessage outputs all the javascript logs to the console to mess up the output of yourself.

If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!
Posted in

Leave a Reply