2012-03-25 44 views
3

我正在學習HTML5並測試Qt混合應用程序的新功能。 現在我正在研究一個簡單的地理位置示例,但是當我調用navigator.geolocation.getCurrentPosition(displayLocation)時;似乎QtWebKit不支持它,但根據這個http://trac.webkit.org/wiki/QtWebKitFeatures22 Qt4.8.0自帶的QtWebKit版本支持地理定位。Qt WebKit和HTML5地理位置

這是代碼我使用:

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; 
} 

而且對Qt的一面:

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; 
    } 

任何人知道如何啓用Html5的地理位置爲Qt的桌面應用程序?

回答

2

您需要子類QWebPage併爲QWebPage::featurePermissionRequested(QWebFrame*, QWebPage::Feature)信號製作信號處理程序。

這裏是一個參考實現:

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); 
     } 
}; 

使用QWebView::setPage()與新創建的子類,使QtWebKit的使用QWebPage實現的實例。

如果您需要更多幫助,請查看作爲WebKit.org存儲庫一部分的QtMiniBrowser。源代碼可從這裏獲得http://trac.webkit.org/browser/trunk/Tools/QtTestBrowser

+0

問題是,它甚至不會調用navigator.geolocation.getCurrentPosition(displayLocation);它找不到navigator.geolocation – userX731 2012-03-26 03:43:06

+0

userX731,你有沒有想過這個? – jdcravens 2013-03-30 14:51:54

+0

我試着用PyQt4來實現這個功能,但featurePermissionRequested信號從來沒有出現過。 – Snorfalorpagus 2014-09-12 19:37:57