Qt QWebView 和从 JavaScript 传递数据

发布于 2024-12-04 10:13:44 字数 2107 浏览 7 评论 0原文

我有一个 Qt 程序,用户可以通过 Google 地图选择位置。我使用一个简单的 HTML 文件来创建地图并在 QWebView 控件中加载该文件:

<!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>

问题是如何在 ajax 请求后将数据传递给 Qt C++ 代码。我知道我可以评估 Qt C++ 中的 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%; 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情泪▽动烟 2024-12-11 10:13:44

是的,扩展窗口对象是正确的方法。

我发现一个问题很好地解释了这个过程: Qt 4.6 将对象和子对象添加到 QWebView 窗口对象(C++ 和 Javascript)

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)

故事与诗 2024-12-11 10:13:44

QWebFrame 有一个方法 QWebFrame::addToJavaScriptWindowObject () 可以用来将 Qt 对象添加到 JS 窗口对象。您可以从 JS 端使用它的属性和插槽。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文