Qt & WebKit Browsers

I don’t recall if it was Nokia or Trolltech who originally decided to add a WebKit widget to Qt. Regardless, its a fantastic addition to the framework and the ramifications to the world are still not yet fully known.

We’re all familiar with web browsers primarily on our PCs and mobile phones. That is going to change… in a big way. Qt has made it simple for development teams to create custom browsers that can run anywhere and inside any application. As a result we’re going to start seeing web browser in our cars, kitchen appliances, digital picture frames, remote controls, and even exercise equipment.

Below you will find an example I wrote using the Qt WebKit support. Its a very simple “custom” browser that allows an application to instantiate the object and render any HTML.

Header file (wijibrowser.h):

#include <QtGui>
#include <QWebView>

class WijiBrowser : public QMainWindow
{
    Q_OBJECT

public:
    WijiBrowser();
    ~WijiBrowser();

    bool loadUrl(QString name, QString url);
    bool loadHTML(QString name, QString html);
    QString contentName() { return m_name; }
    QString url() { return (view ? view->url().toString() : QString("none")); }

protected slots:
    void finishLoading(bool ok);

private:
    QString m_name;
    QWebView *view;
};

CPP file (wijibrowser.cpp):

#include <QtGui>
#include <QtWebKit>
#include "wijibrowser.h"

WijiBrowser::WijiBrowser()
{
    m_name = "unknown";

    QNetworkProxyFactory::setUseSystemConfiguration(true);

    view = new QWebView(this);
    view->load(QUrl("http://www.immersivelabs.com"));
    connect(view, SIGNAL(loadFinished(bool)), SLOT(finishLoading(bool)));

    setCentralWidget(view);
    setUnifiedTitleAndToolBarOnMac(true);
}

WijiBrowser::~WijiBrowser()
{
	delete view;
}

void WijiBrowser::finishLoading(bool ok)
{
	QString status = (ok ? "OK" : "Failed");
	QString url = view->url().toString();
	QString msg = QString("finishLoading %1 - %2").arg(url).arg(status);
	qDebug(msg);
}

bool WijiBrowser::loadUrl(QString name, QString url)
{
	m_name = name;
    view->load(QUrl(url));
}

bool WijiBrowser::loadHTML(QString name, QString html)
{
	m_name = name;
	view->setHtml(html);
}

And here is a simple main.cpp to launch it:

#include <QtGui>
#include "wijibrowser.h"

int main(int argc, char * argv[])
{
    QApplication app(argc, argv);
    WijiBrowser browser;
    browser.show();
    return app.exec();
}

1 thought on “Qt & WebKit Browsers

  1. Pingback: Web Apps – The More Custom The Better | Christopher Piekarski

Comments are closed.