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 509 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.
  • thamT Offline
    thamT Offline
    tham
    wrote on 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
    JonBJ 1 Reply Last reply
    0
    • thamT 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
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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?

      thamT 1 Reply Last reply
      0
      • JonBJ JonB

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

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

        Try spelling script correctly?

        thamT Offline
        thamT Offline
        tham
        wrote on 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
        • thamT Offline
          thamT Offline
          tham
          wrote on 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

          • Login

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