2012-03-25 30 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; 
    } 

誰でもQtのデスクトップアプリのHTML5のジオロケーションを有効にする方法を知っていますか?

答えて

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

+0

userX731を見つけることができません、あなたはこれを理解しましたか? – jdcravens

+0

私はPyQt4を使ってこれを実装しようとしましたが、featurePermissionRequestedシグナルは決して発生しません。 – Snorfalorpagus

関連する問題