Javascript Qt WebKit和HTML5地理定位
我正在学习HTML5,并在Qt混合应用程序上测试新功能。 现在我正在处理一个简单的地理位置示例,但是当我调用navigator.geolocation.getCurrentPosition(displayLocation)时;QtWebKit似乎不支持它,但根据这一点,Qt4.8.0附带的QtWebKit版本支持地理定位 这是我正在使用的代码:Javascript Qt WebKit和HTML5地理定位,javascript,html,qt,geolocation,Javascript,Html,Qt,Geolocation,我正在学习HTML5,并在Qt混合应用程序上测试新功能。 现在我正在处理一个简单的地理位置示例,但是当我调用navigator.geolocation.getCurrentPosition(displayLocation)时;QtWebKit似乎不支持它,但根据这一点,Qt4.8.0附带的QtWebKit版本支持地理定位 这是我正在使用的代码: window.onload = function() { getMyLocation(); } function getMyLoc
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地理定位吗?您需要为
QWebPage
子类,并为请求的QWebPage::FeaturePermissions(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
。源代码可以从这里获得问题是它甚至不能调用navigator.geolocation.getCurrentPosition(displayLocation);它找不到navigator.geolocationuserX731,你知道吗?我试着用PyQt4实现它,但featurePermissionRequested信号似乎从未发出。