2

Using googlemap with QWebEngineView, i would like to add myscript.js in the html file. It works very well in debug, but not in release. In release mode, myscript.js is not found with error js: Uncaught (in promise) ReferenceError: myscriptNameSpace is not defined

I'm working with Qt 5.12.2, MSVC 2017

my qrc file

<RCC>
    <qresource prefix="/">
        <file>html/googlemap.html</file>
        <file>js/myscript.js</file>
    </qresource>
</RCC>

my .h file

class GoogleMapWebEngine : public QWidget
{
    Q_OBJECT

public:
    explicit GoogleMapWebEngine(QWidget *parent = nullptr);
    virtual ~GoogleMapWebEngine() override;

protected:
    virtual void wheelEvent(QWheelEvent * event) override;

private:
    Ui::GoogleMapWebEngine *ui;
    QWebEngineView* m_webview;
    QWebChannel* m_webChannel;
};

my .cpp file

GoogleMapWebEngine::GoogleMapWebEngine(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GoogleMapWebEngine)
{
    qputenv("QTWEBENGINE_REMOTE_DEBUGGING", "1234");
    ui->setupUi(this);

    m_webview = new QWebEngineView();
    m_webChannel = new QWebChannel();
    m_webChannel->registerObject("goolemainWindow", this);
    m_webview->page()->setWebChannel(m_webChannel);

    QUrl url = QUrl("qrc:/html/googlemap.html");
    m_webview->setUrl(url);
    ui->verticalLayout->addWidget(m_webview);
}

my googlemap.html file

<html>
    <head>
        ...
        <script src="qrc:/js/myscript.js"></script>

        <script type="text/javascript">           
            function initMap()
            {
                var
                    mapCenter = [32.794488, -96.780372],
                    mapOptions = {
                        zoom: 18,
                        center: new google.maps.LatLng(mapCenter[0], mapCenter[1]),
                        mapTypeId: google.maps.MapTypeId.SATELLITE

                    },
                    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
                    myscriptNameSpace.test();
            }
        </script>        
    </head>

    <body ondragstart="return false">
        <div id="map-canvas" />
        <script async defer src="https://maps.googleapis.com/maps/api/js?key=myKey&callback=initMap"></script>
    </body>
</html>

myscript.js

var myscriptNameSpace = {}
myscriptNameSpace.test = function(){
    alert('test');
}

Expected : Alert message must be displayed

Current error : js: Uncaught (in promise) ReferenceError: myscriptNameSpace is not defined

My full source code is found here : github.com/hunglxtp/googlemapTest

LE Xuan Hung
  • 51
  • 1
  • 6
  • are you sure that qt actually loads "myscript.js" in the page? it seems like it's not working in your "myscript.js" there is actually a space between "myscriptNameSpace" and ".test" or it's a typo? – Margon Jul 05 '19 at 09:49
  • yes, in Debug, it works well. But in release, i doubt that it is loaded but i don't know why. The space is just an erreur of editing on the page. I edited the question and removed the space – LE Xuan Hung Jul 05 '19 at 09:53
  • unfortunately I never worked with qt, but upvoted and whish you luck! – Margon Jul 05 '19 at 10:02
  • Thanks you Margon. – LE Xuan Hung Jul 05 '19 at 10:09
  • @LEXuanHung: I'm unable to build your example. Header files are missing. I don't have `Ui::GoogleMapWebEngine`. Please create a minimal reproducible example. – Aleph0 Jul 05 '19 at 11:02
  • Thank you, my sourcecode complete is here : https://github.com/hunglxtp/googlemapTest – LE Xuan Hung Jul 05 '19 at 12:54
  • @LEXuanHung: I compiled your code. There is the javascript message box and then the webview with google maps is loaded as desired. Are you getting some more output on the console? It might be also a timing problem, where you have to wait till the WebEngine is ready. I also suggest that you try to make a rebuild all of your application. – Aleph0 Jul 05 '19 at 19:23
  • Yes, that is what i have too when i'm in Debug : a javascript message box 'test'. But no message box when i'm in Release, and in the console, i have that error message : `js: Uncaught (in promise) ReferenceError: myscriptNameSpace is not defined` – LE Xuan Hung Jul 06 '19 at 01:07

1 Answers1

2

I finally found the problème from qt documentation. My solution is to add : QTQUICK_COMPILER_SKIPPED_RESOURCES += testgooglemapwebengine.qrc in the .pro file.

LE Xuan Hung
  • 51
  • 1
  • 6