QtWebkit : Capturing javascript errors



  • Hello All,

    I'm just beginning with qt and qtwebkit so please forgive my newbness...

    Is it possible to capture javascript errors from qtwebkit?

    I have seen advice online that says to subclass QWebPage, and override javaScriptConsoleMessage.

    What I've done is to create a new HTML5 project in Qt Creator (which generates the Html5ApplicationViewerPrivate class), then create a new class, RRWebPage, and substitute that in place of QWebPage in Html5ApplicationViewerPrivate.

    In RRWebPage I have overridden javaScriptConsoleMessage, using qDebug to output a message and I have inserted a breakpoint into the function.

    I start debugging my application, I see some javascript alerts that I have put in, and then it hits a deliberate javascript error - but nothing happens... My breakpoint is not hit and nothing is logged... Other breakpoints are hit when the application is initializing so there is no problem with breakpoints.

    Can anyone shed any light on this please? Is this the correct way to capture javascript errors?

    I'm using Qt Creator 2.4.1, Build configuration Qt 4.8.1 for Desktop - MSVC2010 Debug

    Many thanks in advance,

    Peter



  • you mean javaScriptConsoleMessage is never called? maybe something wrong with function overriding or something else... I can only guess. If you can show us your code, it will help allot.



  • Sorry yes, thats what I mean,

    Here is my subclass of QWebPage:

    RRWebPage.h
    @#ifndef RRWEBPAGE_H
    #define RRWEBPAGE_H

    #include <QWebPage>

    class RRWebPage : public QWebPage
    {
    Q_OBJECT
    public:
    explicit RRWebPage(QObject *parent = 0);

    void test();
    

    protected:
    void javaScriptConsoleMessage( const QString & message, int lineNumber, const QString & sourceID );

    signals:

    public slots:

    };

    #endif // RRWEBPAGE_H@

    RRWebPage.cpp
    @#include "rrwebpage.h"
    #include <QDebug>
    #include <QDate>

    RRWebPage::RRWebPage(QObject *parent) :
    QWebPage(parent)
    {
    }

    void RRWebPage::test()
    {
    //say hello!
    qDebug() << "Date:" << QDate::currentDate();
    }

    void RRWebPage::javaScriptConsoleMessage( const QString & message, int lineNumber, const QString & sourceID )
    {
    //do something!
    qDebug() << message << lineNumber << sourceID;
    }@

    Here is where I assign an instance of RRWebPage in html5applicationviewer.cpp:

    @NavigationControllerPrivate::NavigationControllerPrivate(QWidget *parent, QGraphicsWebView *webView)
    : m_webPage(0)
    , m_webWidget(0)
    , m_graphicsWebView(webView)
    , m_webNavigation(0)
    {
    Q_UNUSED(parent);
    m_graphicsWebView->setAcceptTouchEvents(true);
    m_webPage = new RRWebPage;
    m_graphicsWebView->setPage(m_webPage);
    m_webNavigation = new WebNavigation(m_graphicsWebView, m_webPage);
    m_webNavigation->setParent(m_graphicsWebView);
    }@

    Thanks for your help,

    Peter



  • Hm, simple test worked for me...
    So, no problem on QWebPage side...

    Just to be sure what you actually should get any message through javaScriptConsoleMessage, test it like this:

    connect to the webview signal loadFinished(bool), and in the connected slot execute following js:

    @<webview>->page()->mainFrame()->evaluateJavaScript("console.log('testing test');");@

    you should then see "testing test" in debug console or get breakpoint.



  • Hi again,

    Sorry for the delay, I got pulled onto another project but am resuming this now - 
    
    I have done as you requested, but javaScriptConsoleMessage does not get fired. I have verfied that 
    

    @<webview>->page()->mainFrame()->evaluateJavaScript("console.log('testing test');");@

    Is definitely running.
    

    Thanks,

    Peter



  • Ti be clear. if you run @evaluateJavaScript("console.log('testing test');");@ you see message "testing test" in your reimplemented javaScriptConsoleMessage function, right?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.