【问题标题】:Qt WebKit and HTML5 geolocationQt WebKit 和 HTML5 地理定位
【发布时间】:2012-03-25 15:27:02
【问题描述】:

我正在学习 HTML5 并在 Qt 混合应用程序上测试新功能。 现在我正在处理一个简单的地理定位示例,但是当我调用 navigator.geolocation.getCurrentPosition(displayLocation);貌似QtWebKit不支持,不过根据http://trac.webkit.org/wiki/QtWebKitFeatures22的说法,Qt4.8.0自带的QtWebKit版本支持地理定位。

这是我正在使用的代码:

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 地理定位功能吗?

【问题讨论】:

    标签: javascript html qt geolocation


    【解决方案1】:

    您需要继承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 实现。

    如果您需要更多帮助,请查看 QtMiniBrowser,它是 WebKit.org 存储库的一部分。源代码可从这里获得http://trac.webkit.org/browser/trunk/Tools/QtTestBrowser

    【讨论】:

    • 问题是它甚至无法调用 navigator.geolocation.getCurrentPosition(displayLocation);找不到navigator.geolocation
    • userX731,你有想过这个吗?
    • 我尝试使用 PyQt4 实现此功能,但 featurePermissionRequested 信号似乎从未触发。
    猜你喜欢
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 2011-03-24
    • 2015-03-27
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    相关资源
    最近更新 更多