Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Include js files to the web app created by Qt
Forum Updated to NodeBB v4.3 + New Features

Include js files to the web app created by Qt

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 503 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.
  • T Offline
    T Offline
    tham
    wrote on 13 Sept 2019, 09:00 last edited by tham
    #1

    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
    J 1 Reply Last reply 13 Sept 2019, 09:41
    0
    • T tham
      13 Sept 2019, 09:00

      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
      J Offline
      J Offline
      JonB
      wrote on 13 Sept 2019, 09:41 last edited by
      #2

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

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

      Try spelling script correctly?

      T 1 Reply Last reply 13 Sept 2019, 10:08
      0
      • J JonB
        13 Sept 2019, 09:41

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

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

        Try spelling script correctly?

        T Offline
        T Offline
        tham
        wrote on 13 Sept 2019, 10:08 last edited by
        #3

        @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
        0
        • T Offline
          T Offline
          tham
          wrote on 13 Sept 2019, 10:27 last edited by
          #4

          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
          0

          1/4

          13 Sept 2019, 09:00

          • Login

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