Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved Include js files to the web app created by Qt

    General and Desktop
    2
    4
    160
    Loading More Posts
    • 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.
    • tham
      tham last edited by tham

      I would like to call the js libraries by EM_JS, I try to manually include the scripts in the html file(app_name.html ) after compiled. Like

      app_name.html

      <scirpt src="js/config.js"><script>
      

      js/config.js

      window._config =  {
      
      api : {
      
      invokeUrl: 'abcd"
      
      }
      
      }
      

      mainwindow.cpp

      ``'
      EM_JS(char*, get_invoke_url, (), {
      var jsString = window._config.api.invokeUrl;
      var lengthBytes = lengthBytesUTF8(jsString)+1;
      var stringOnWasmHeap = _malloc(lengthBytes);
      stringToUTF8(jsString, stringOnWasmHeap, lengthBytes);
      return stringOnWasmHeap;
      })

      void process_input_text::on_pushButtonSelectCharsInFile_clicked()
      {
      char *url = get_invoke_url();
      ui->lineEditCharsInFile->setText(url);
      free(url);
      }

      
      After I call the on_pushButtonSelectCharsInFile_clicked, it give me error message 
      
      **TypError : window._config is undefined**
      
      How should I include the js file in order to call it from c++?Thanks
      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @tham last edited by

        @tham said in Include js files to the web app created by Qt:

        <scirpt src="js/config.js"><script>

        Try spelling script correctly?

        tham 1 Reply Last reply Reply Quote 0
        • tham
          tham @JonB last edited by

          @jonb Thanks, tried it, but still the same.

          I include the script like

          <script src='config.js'></script>
          <script type='text/javascript'>
          //codes generated by Qt
          </script>
          

          different part is I change window to Module
          Now it complain Module._config is undefined

          1 Reply Last reply Reply Quote 0
          • tham
            tham last edited by

            Wonder if it is caused by name conflict etc, so I change it to

            var _global_config =  {
            
            api : {
            
            invokeUrl: 'abcd"
            
            }
            
            }
            

            call it in EM_JS as

            _global_config.api.invokeUrl
            

            Ok, at last it works. Maybe window._config and Module._config works too, maybe I was forgot to update the html or wrong spelling etc, I am not sure, sometimes this kind of small errors are hard to spot, hope someday we could have compile time error for them.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post