Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. QWebEngineView can not find javascript file in release but ok in Debug
QtWS25 Last Chance

QWebEngineView can not find javascript file in release but ok in Debug

Scheduled Pinned Locked Moved Solved QtWebEngine
javascriptreference errorqwebengineqwebengineview
2 Posts 1 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hunglx
    wrote on last edited by Hunglx
    #1

    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 messagae ‘test’ must be displayed

    But have error in console in release : js: Uncaught (in promise) ReferenceError: myscriptNameSpace is not defined

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

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hunglx
      wrote on last edited by
      #2

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

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved