Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Redirecting JavaScript console messages in a Qt hybrid application

From Wiki
Jump to: navigation, search
Article Metadata
Created: isalento (25 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)


One of the first things that you encounter when creating a hybrid application on top of QtWebKit is lack of JavaScript debug information. While it is easy to use Web Inspector to debug applications, on some occasions it is enough to get debug messages from the hybrid application for later inspection. This article shows how to redirect JavaScript console messages into a file or your own message handler.

Redirecting JavaScript console messages can be easily done by sub classing QWebPage and overriding virtual function called javaScriptConsoleMessage(). Writing to file can be omitted, if you have already implemented your own message handler that redirects qDebug –messages into a file


#ifndef MYPAGE_H
#define MYPAGE_H
#include <QObject>
#include <QWebPage>
class MyPage : public QWebPage
MyPage(QObject * parent = 0 );
void javaScriptConsoleMessage(const QString& message, int lineNumber, const QString& sourceID);
#endif // MYPAGE_H


#include "mypage.h"
#include <QDebug>
#include <QFile>00
#include <QTextStream>
MyPage::MyPage(QObject *parent): QWebPage(parent){
void MyPage::javaScriptConsoleMessage(const QString& message, int lineNumber, const QString& sourceID){
QString logEntry = message +" on line:"+ QString::number(lineNumber) +" Source:"+ sourceID;
/** can be omitted */
QFile file("C:\\Data\\debug.txt");
if (! | QIODevice::Append | QIODevice::Text))
QTextStream out(&file);
out << logEntry << endl;

After you have finished creating your own subclass of QWebPage, you still need to set it as active page on your QWebView.

MyPage page = new MyPage();
This page was last modified on 11 October 2012, at 01:18.
133 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.