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. Qt6.5 QWebEngineView html script
Forum Update on Monday, May 27th 2025

Qt6.5 QWebEngineView html script

Scheduled Pinned Locked Moved Unsolved QtWebEngine
9 Posts 5 Posters 1.1k 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.
  • M Offline
    M Offline
    MYKJ
    wrote on 10 Mar 2023, 05:00 last edited by
    #1

    Hello, any one can help me ? Thank you!

    My project load local html with QWebEngineView, and it work well with Qt5.9.2 and Safari/Chrome.

    Now, I update Qt to 6.5.0, and it cannot work with error " js: Uncaught ReferenceError: L is not defined", It looks like cannot parse script src.

    Here is the html

    <html>
        <head>
            <meta charset="utf-8" />
            <style type="text/css">
                body, html{width: 100%;height: 100%;margin:0;}
                #map_canvas{width:100%;height:100%;}
            </style>
            <link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
        </head>
        <script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
        
        <script language="javascript">
            
            function init() {
                var map = new L.Map('map_canvas');
                
                L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                    attribution: '&copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors',
                    maxZoom: 18
                }).addTo(map);
                map.attributionControl.setPrefix(''); // Don't show the 'Powered by Leaflet' text.
                
                var london = new L.LatLng(51.505, -0.09); // geographical point (longitude and latitude)
                map.setView(london, 13);
            }
        </script>
        
        <body onLoad="javascript:init();">
            <div id="map_canvas"></div>
        </body>
    </html>
    
    

    and here is the code about webengineview

           QWebEngineProfile profile;
         profile.installUrlSchemeHandler(QByteArrayLiteral("webui"), &handler);
    
        QWebEnginePage page(&profile);
        QWebEngineView view;
        QWebEngineSettings *settings = view.settings();
        //开启插件支持(如Flash player、文件拖拽等)
        settings->setAttribute(QWebEngineSettings::PluginsEnabled, true);
        settings->setAttribute(QWebEngineSettings::AllowWindowActivationFromJavaScript, true);
        settings->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
        settings->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, true);
        settings->setAttribute(QWebEngineSettings::JavascriptCanPaste, true);
        settings->setAttribute(QWebEngineSettings::JavascriptCanAccessClipboard, true);
        settings->setAttribute(QWebEngineSettings::XSSAuditingEnabled, true);
        settings->setAttribute(QWebEngineSettings::LinksIncludedInFocusChain, true);
        settings->setAttribute(QWebEngineSettings::PdfViewerEnabled, true);
        settings->setUnknownUrlSchemePolicy(QWebEngineSettings::AllowAllUnknownUrlSchemes);
        
        view.setPage(&page);
        page.load(QUrl("qrc:/openstreet.html"));
        view.setContextMenuPolicy(Qt::NoContextMenu);
        view.resize(500, 600);
    
        view.show();
    
    
    J 1 Reply Last reply 10 Mar 2023, 05:09
    0
    • M MYKJ
      10 Mar 2023, 05:00

      Hello, any one can help me ? Thank you!

      My project load local html with QWebEngineView, and it work well with Qt5.9.2 and Safari/Chrome.

      Now, I update Qt to 6.5.0, and it cannot work with error " js: Uncaught ReferenceError: L is not defined", It looks like cannot parse script src.

      Here is the html

      <html>
          <head>
              <meta charset="utf-8" />
              <style type="text/css">
                  body, html{width: 100%;height: 100%;margin:0;}
                  #map_canvas{width:100%;height:100%;}
              </style>
              <link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
          </head>
          <script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
          
          <script language="javascript">
              
              function init() {
                  var map = new L.Map('map_canvas');
                  
                  L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                      attribution: '&copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors',
                      maxZoom: 18
                  }).addTo(map);
                  map.attributionControl.setPrefix(''); // Don't show the 'Powered by Leaflet' text.
                  
                  var london = new L.LatLng(51.505, -0.09); // geographical point (longitude and latitude)
                  map.setView(london, 13);
              }
          </script>
          
          <body onLoad="javascript:init();">
              <div id="map_canvas"></div>
          </body>
      </html>
      
      

      and here is the code about webengineview

             QWebEngineProfile profile;
           profile.installUrlSchemeHandler(QByteArrayLiteral("webui"), &handler);
      
          QWebEnginePage page(&profile);
          QWebEngineView view;
          QWebEngineSettings *settings = view.settings();
          //开启插件支持(如Flash player、文件拖拽等)
          settings->setAttribute(QWebEngineSettings::PluginsEnabled, true);
          settings->setAttribute(QWebEngineSettings::AllowWindowActivationFromJavaScript, true);
          settings->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
          settings->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, true);
          settings->setAttribute(QWebEngineSettings::JavascriptCanPaste, true);
          settings->setAttribute(QWebEngineSettings::JavascriptCanAccessClipboard, true);
          settings->setAttribute(QWebEngineSettings::XSSAuditingEnabled, true);
          settings->setAttribute(QWebEngineSettings::LinksIncludedInFocusChain, true);
          settings->setAttribute(QWebEngineSettings::PdfViewerEnabled, true);
          settings->setUnknownUrlSchemePolicy(QWebEngineSettings::AllowAllUnknownUrlSchemes);
          
          view.setPage(&page);
          page.load(QUrl("qrc:/openstreet.html"));
          view.setContextMenuPolicy(Qt::NoContextMenu);
          view.resize(500, 600);
      
          view.show();
      
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 10 Mar 2023, 05:09 last edited by
      #2

      @MYKJ said in Qt6.5 QWebEngineView html script:

      var map = new L.Map('map_canvas');

      What is L?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 10 Mar 2023, 05:43
      0
      • J jsulm
        10 Mar 2023, 05:09

        @MYKJ said in Qt6.5 QWebEngineView html script:

        var map = new L.Map('map_canvas');

        What is L?

        M Offline
        M Offline
        MYKJ
        wrote on 10 Mar 2023, 05:43 last edited by
        #3

        @jsulm L is defined from <script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>, the problem looks like cannot parse this script.

        R 1 Reply Last reply 14 Mar 2023, 03:57
        0
        • M MYKJ
          10 Mar 2023, 05:43

          @jsulm L is defined from <script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>, the problem looks like cannot parse this script.

          R Offline
          R Offline
          rockyxia
          wrote on 14 Mar 2023, 03:57 last edited by
          #4

          @MYKJ leaflet.js is not defined L

          M 1 Reply Last reply 17 Mar 2023, 14:38
          0
          • R rockyxia
            14 Mar 2023, 03:57

            @MYKJ leaflet.js is not defined L

            M Offline
            M Offline
            MYKJ
            wrote on 17 Mar 2023, 14:38 last edited by
            #5

            @rockyxia If L is not defined, why it can work normally wifh safari and chrome?

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fildaw
              wrote on 22 Dec 2024, 22:25 last edited by
              #6

              Hello, were you able to fix this problem? I'm experiencing the same error using Qt6 WebView.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jennif56
                wrote on 17 Jan 2025, 09:15 last edited by
                #7

                hello i have je same problem
                my code
                main.cpp

                #include <QApplication>
                #include <QWebEngineView>
                
                int main(int argc, char *argv[]) {
                    QApplication app(argc, argv);
                
                    // Créer une instance de QWebEngineView
                    QWebEngineView webView;
                
                    // Charger la page HTML locale contenant la carte Leaflet
                    webView.setUrl(QUrl::fromLocalFile("/leaflet_map.html")); // Replace with the path of your HTML file
                
                    // Afficher la fenêtre
                    webView.resize(800, 600); 
                    webView.show();
                
                    return app.exec();
                }
                

                leaflet_map.html

                <!DOCTYPE html>
                <html>
                <head>
                    <title>Carte Leaflet</title>
                    <meta charset="utf-8" />
                    <meta name="viewport" content="width=device-width, initial-scale=1.0">
                    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
                    <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
                    <style>
                        #map { height: 100%; }
                        html, body { height: 100%; margin: 0; }
                    </style>
                </head>
                <body>
                    <div id="map"></div>
                    <script>
                        var map = L.map('map').setView([43.6108, 3.8767], 13); 
                        L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                            attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
                        }).addTo(map);
                    </script>
                </body>
                </html>
                

                I use Qt6.8.1 with the MSVC2022_64bit kit
                I tested the html here https://html.onlineviewer.net/ and it works correctly.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jennif56
                  wrote on 23 Jan 2025, 07:21 last edited by
                  #8

                  I found a solution to the problem: set QWebEngineSettings::LocalContentCanAccessRemoteUrls to true
                  add this to main.cpp file

                  #include <QWebEngineSettings>
                  webView.page()->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
                  

                  the link to the doc https://doc.qt.io/qt-6/qwebenginesettings.html

                  1 Reply Last reply
                  1
                  • F Offline
                    F Offline
                    fildaw
                    wrote on 30 Jan 2025, 16:15 last edited by
                    #9

                    Thank you, it seems to be the case. Do you happen to know how to set this attribute in QML? I need to modify part of extension (plasmoid) for KDE Plasma, it uses QML to setup the WebView.

                    1 Reply Last reply
                    0

                    • Login

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