3

I’m learning HTML5 and testing the new features on a Qt hybrid application. Now I’m working on a simple geolocation example but when I call navigator.geolocation.getCurrentPosition(displayLocation); it seems the QtWebKit does not support it, but acording to this http://trac.webkit.org/wiki/QtWebKitFeatures22 the version of QtWebKit that comes with Qt4.8.0 supports geolocation.

This is the code I’m using:

window.onload = function()
{
    getMyLocation();      
}

function getMyLocation()
{
    if(navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(displayLocation);        
    }  
    else
    {
        alert("No geolocation support");  
    }
}

function displayLocation(position)
{
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;

    var div = document.getElementById("location");

    div.innerHTML = "You are at Latitude: " + latitude + ", Longitude: " + longitude;  
}

And on the Qt side:

QWebView* MyWindow::createWebView()
    {
        QWebSettings* default_settings = QWebSettings::globalSettings();
        default_settings->setAttribute(QWebSettings::JavascriptEnabled,true);
        default_settings->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled,true);
        default_settings->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled,true);
        default_settings->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,true);        
        default_settings->setAttribute(QWebSettings::LocalStorageEnabled,true);
        default_settings->setAttribute(QWebSettings::JavascriptCanAccessClipboard,true);
        default_settings->setAttribute(QWebSettings::DeveloperExtrasEnabled,true);

        QWebView* web_view = new QWebView(this);

        connect(web_view->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
                this, SLOT(addJavascriptObject()));

        inspector_->setPage(web_view->page());

        inspector_->setVisible(true);
        inspector_->show();

        web_view->load(QUrl("qrc:/html/geolocation_example.html"));

        return web_view;
    }

Anyone knows how to enable Html5 geolocation for a Qt desktop app?

1 Answer 1

2

You need to subclass QWebPage and make a signal handler for the QWebPage::featurePermissionRequested(QWebFrame*, QWebPage::Feature) signal.

Here is a reference implementation:

class WebPage : public QWebPage
{
    Q_OBJECT

    public:
        WebPage(QObject* parent = 0) :
            QWebPage(parent)
        {
            connect(this, SIGNAL(featurePermissionRequested(QWebFrame*, QWebPage::Feature)), SLOT(permissionRequested(QWebFrame*, QWebPage::Feature)));
        }

        virtual ~WebPage()
        {
        }

    private slots:
        void permissionRequested(QWebFrame* frame, QWebPage::Feature feature)
        {
            setFeaturePermission(frame, feature, PermissionGrantedByUser);
        }
};

Use QWebView::setPage() with an instance of your newly created subclass to make QtWebKit use your QWebPage implementation.

In case you need more help, have a look at the QtMiniBrowser which is part of the WebKit.org repository. The source code is available from here http://trac.webkit.org/browser/trunk/Tools/QtTestBrowser

3
  • The problem is that it doesn't even get to call navigator.geolocation.getCurrentPosition(displayLocation); It can't find navigator.geolocation
    – userX731
    Commented Mar 26, 2012 at 3:43
  • userX731, did you ever figure this out?
    – jdcravens
    Commented Mar 30, 2013 at 14:51
  • I tried implementing this using PyQt4, but the featurePermissionRequested signal never seems to fire. Commented Sep 12, 2014 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.