开发者

Qt QWebView and passing data from JavaScript

I've got a Qt program where a user can select the location by Google Maps. I'm using a simple HTML file for creating the map and load this file in the QWebView control:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        html
        {
            height: 100%;
        }
        body
        {
            height: 100%;
            margin: 0;
            padding: 0;
        }
        #map_canvas
        {
            height: 100%;
        }
    </style>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

    <script type="text/javascript">
        function initialize() {
            var latlng = new google.maps.LatLng(-34.397, 150.644);

            var myOptions = {
                zoom: 8,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

            google.maps.event.addListener(map, "click", function (event) {

                var geoLocationUrl = 'http://maps.googleapis.com/maps/api/geocode/xml?latlng='
                + event.latLng.lat() + "," + event.latLng.lng() + "&sensor=false";

                console.log(geoLocationUrl);

                $.ajax({
                    type: "GET",
                    url: geoLocationUrl,
                    dataType: "xml",
                    success: function (xml) {

                    },
                    error: function () {

                    }


                });
            });
        }

        function showLocation(location) {

        }

    </script>
</head>
<body onload="initialize()">
    <div id="map_canvas" style="width: 100%; heigh开发者_如何学Pythont: 100%">
    </div>
</body>
</html>

The question is how to pass data after an ajax request to the Qt C++ code. I know that I can evalute a javascript function in Qt C++ but in this case the task is reverse.


Yes, extending the window-object is the way to go.

I found a question which explains the process quite nicely: Qt 4.6 Adding objects and sub-objects to QWebView window object (C++ & Javascript)


QWebFrame has a method QWebFrame::addToJavaScriptWindowObject() you can use to add a Qt object to the JS window object. You can use its properties and slots from the JS side.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜