繁体   English   中英

Qt Webengine 崩溃并显示白屏

[英]Qt Webengine crash and Shows white screen

我正在Qt 中开发一个 Google map 应用程序,该应用程序跟踪用户 position 并绘制标记并将标记与折线连接。 有时页面显示白屏。 我正在使用Qt 5.12.11 和 Windows 10

观察:

  • 当标记添加到 map 时,web 引擎 memory 不断增加。
  • 崩溃:当出现白屏时,web 引擎没有运行。

这是示例代码。

webViewOnQml.pro

QT += quick webengine

CONFIG += c++11 qml_debug
DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += \
        main.cpp

RESOURCES += qml.qrc

主文件

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtWebEngine/QtWebEngine>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);

    QGuiApplication app(argc, argv);
    QtWebEngine::initialize();
    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/res/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

main.qml

import QtQuick 2.5
import QtQuick.Window 2.2
import QtWebEngine 1.0
import QtWebChannel 1.0

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Google Map")

    WebEngineView {
        id: view
        anchors.fill: parent
        url: "googleMaps.html"
    }
}

googleMaps.html

    <!-- Make sure to enter a valid Google Key in the Google Script at the bottom of this page -->
    <!-- Visit https://developers.google.com/maps/documentation/javascript/get-api-key -->
    <html>
     <head>
       <style type="text/css">
         html, body { height: 100%; margin: 0; padding: 0; }
         #map { height: 100%; width:100%; margin:0; padding:0; }
       </style>
     </head>
     <body>
        <button onclick="findLocation(-22.834418, 14.862920)">click</button>
       <div id="map"></div>
       <script>
         var markersArray = [];
         setTimeout(() => {
                 let mapUrl = document.createElement("script");
                 let url = "https://maps.googleapis.com/maps/api/js?key=<YOUR_API_KEY>&v=3&callback=initMap"
                 mapUrl.setAttribute("src", url);
                 mapUrl.setAttribute("async","true");
                 mapUrl.setAttribute("defer", "defer");
                 document.body.appendChild(mapUrl);
         }, 50);

         function initMap() {
           myPosition =  new google.maps.LatLng(0,0)
           map = new google.maps.Map(document.getElementById('map'), {
                center:myPosition,
                streetViewControl: false,
                draggable: true,
                scrollwheel: true,
                panControl: true,
                zoom: 17,
                mapTypeId: 'satellite',
                disableDefaultUI: true,
                zoomControl:true,
                mapTypeControl:true,
                rotateControl: true,
                tilt:0
            })
            dotIcon = {  type:'Symbol',
                path: google.maps.SymbolPath.CIRCLE,
                scale: 3,
                fillColor: '#196ffa',
                strokeColor: '#196ffa',
                fillOpacity: 1.0,
                strokeWeight: 0.5,
                zIndex:99,
            }
            latestDotIcon = {  type:'Symbol',
                path: google.maps.SymbolPath.CIRCLE,
                scale: 3,
                fillColor: '#10ff03',
                strokeColor: '#10ff03',
                fillOpacity: 1.0,
                strokeWeight: 2
            }
              addLine();
              currentMarker = new google.maps.Marker({
                    position:new google.maps.LatLng(0,0),
                    icon:latestDotIcon,
                    zIndex:99,
                    map: map
                  });
         }

           function addLine() {
               poly = new google.maps.Polyline({
                   strokeColor: "#000dff",
                   strokeOpacity: 1.0,
                   strokeWeight: 1,
                   zIndex:99,
                 });
                poly.setMap(map);
           }
        function findLocation(latitude,longitude){
            let latLngVal = new google.maps.LatLng(latitude,longitude);
                   marker = new google.maps.Marker({
                      position: latLngVal,
                      icon:dotIcon,
                      map: map
                    });
                    marker.setMap(map);
                    map.setCenter(marker.getPosition());
                    path = poly.getPath();
                    path.push(latLngVal);
                    changeLocation(latitude,longitude);
                    currentMarker.setPosition(latLngVal);
                    delete latLngVal;
                    delete marker;
            }

        function changeLocation(latitude,longitude){
            var lat = latitude+.01
            var lon = longitude+.01
            if(lat>=90) {
                lat=-90
            }
            if(lon>=180) {
                lon=-180
            }
            setTimeout(() => {  findLocation(lat,lon); }, 200);
        }
      </script>
     </body>
    </html>

注意:- 在 googleMaps.html 中的“<YOUR_API_KEY>”中替换您的 Google 地图 API 键以运行代码。

我错过了什么配置吗? 如何避免 ZE8801102A40AD89DDCFDCAEBF008D25Z Web 引擎中的崩溃和 memory 泄漏?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM