2

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%; height: 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.

shadeglare
  • 7,006
  • 7
  • 47
  • 59

2 Answers2

2

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)

Community
  • 1
  • 1
Michael Sandino
  • 1,818
  • 1
  • 13
  • 10
1

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.

Steffen
  • 2,888
  • 19
  • 19
  • How can I pass data from C++ methods to Javascript here. I mean the callbacks. Can you answer this question please - http://stackoverflow.com/questions/22006667/in-qtwebkit-how-to-install-the-callback-from-c-to-javaobject-window – dexterous Feb 25 '14 at 09:26