2012-03-25 77 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