Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to register a class in a .js file of QML using C++?
Forum Updated to NodeBB v4.3 + New Features

How to register a class in a .js file of QML using C++?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 94 Views 2 Watching
  • 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.
  • Y Offline
    Y Offline
    yangsiyu
    wrote last edited by yangsiyu
    #1

    How to register a class in a .js file of QML using C++? For example, I have an OaiClient in C++ and want to use ‘const client = new OaiClient’ in JavaScript.
    I checked the documentation, which states: You cannot use the globalObject() function to change the global object of a QQmlEngine.
    What should I do?

    1 Reply Last reply
    1
    • Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote last edited by
      #2

      Good morning and welcome to the Qt Forum,
      how have you written your C++ extension?
      If you follow this documentation, it will be accessible from QML and Javascript.

      Software Engineer
      The Qt Company, Oslo

      Y 2 Replies Last reply
      0
      • Axel SpoerlA Axel Spoerl

        Good morning and welcome to the Qt Forum,
        how have you written your C++ extension?
        If you follow this documentation, it will be accessible from QML and Javascript.

        Y Offline
        Y Offline
        yangsiyu
        wrote last edited by
        #3

        @Axel-Spoerl Thank you for your reply. I have read this document. What I want to do is something similar to the following:

        import QtQuick
        
        import "backend.js" as Backend
        
        Item {
            Component.onCompleted: {
                Backend.func()
            }
        }
        
        // backend.js
        function func() {
            const obj = new OaiClient()  // OaiClient is a JS class registered from C++
        }
        

        What I want is to be able to register a C++ class to a JS file, and the JS file can use the new keyword to instantiate this class.

        1 Reply Last reply
        0
        • Axel SpoerlA Axel Spoerl

          Good morning and welcome to the Qt Forum,
          how have you written your C++ extension?
          If you follow this documentation, it will be accessible from QML and Javascript.

          Y Offline
          Y Offline
          yangsiyu
          wrote last edited by
          #4

          @Axel-Spoerl I found that the following code works:

             QQmlApplicationEngine engine;
              auto ctor = engine.evaluate(R"(
                  (function() {
                      this.name = "test"
                      this.create = function(){
                          return "hello world!"
                      }
                  })
              )");
              engine.globalObject().setProperty("OaiClient", ctor);
          

          However, I don’t understand what the documentation means when it says that the globalobject properties should not be modified, and I’m unsure what the implications of doing so might be.

          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