Namespaces
Variants
Actions
Revision as of 04:18, 11 October 2012 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Redirecting JavaScript console messages in a Qt hybrid application

Jump to: navigation, search
Article Metadata

Article
Created: isalento (25 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)

Overview

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

mypage.h

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


mypage.cpp

#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;
qDebug()<<logEntry;
 
/** can be omitted */
QFile file("C:\\Data\\debug.txt");
if (!file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
return;
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->webView->setPage(page);
247 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved