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.

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

How can I pass data from C++ methods to Javascript here. I mean the callbacks. Can you answer this question please - stackoverflow.com/questions/22006667/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.