我使用此代码从 javascript 获取数据:
const QVariant myvar = this->page()->mainFrame()->
evaluateJavaScript("measureDistance("+QString::fromStdString(prev->marker_id)+","+QString::fromStdString(stop->marker_id+");"));
bool ok;
const int myint = myvar.toInt(&ok);
if (!ok)
qWarning() << "Error getting int from JS";
qDebug() << myint;
getMapTimeDistance(location, QString::fromStdString(prev->getName()), stop, 10);
在 javascript 中,最后一行是:
function measureDistance(m1, m2) {
var wp = new Array();
var lstart = new google.maps.LatLng(markers[m1].position);
var lend = new google.maps.LatLng(markers[m2].position);
var request = {
origin : lstart,
destination : lend,
waypoints : [],
optimizeWaypoints: true,
travelMode : google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status== google.maps.DirectionsStatus.OK) {
response.routes[0].legs[0].duration.text;
} else {
document.getElementById('locationD').value = "drawRouteBad";
}
});
}
你知道为什么这段代码对我不起作用吗?我尝试了不同的类型组合,但没有任何帮助。可能我不知道 javasscripts 值是什么类型。也许我应该以不同的方式将它传递给 qt?