Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. glitch in javascript bindings
QtWS25 Last Chance

glitch in javascript bindings

Scheduled Pinned Locked Moved Unsolved Language Bindings
7 Posts 2 Posters 1.2k 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.
  • S Offline
    S Offline
    skylendar
    wrote on last edited by
    #1

    Hi there and thx for reading and answering this post in you can.

    I shouldn't ask you that because it worked before but doesn't now. Why ????

    I declare 2 C++ classes:

    class z: public QObject
    {
        Q_OBJECT
     public:
        Q_INVOKABLE z() {}
    };
    
    class test: public QObject
    {
        Q_OBJECT
     public:
        Q_INVOKABLE test() {}
    public slots:
        z* get() { auto x = new z; return x; }
    };
    `
    

    Then the corresponding declarations for QJSEngine:

    
    
    QJSValue ZObj = newQMetaObject(&z::staticMetaObject);
     globalObject().setProperty("z", ZObj);
     QJSValue TestObj = newQMetaObject(&test::staticMetaObject);
     globalObject().setProperty("test", TestObj);
    

    so far so good, but when I run a javascript script such as :

    var z = new test()
    var a = z.get()
    

    QJSEngine returns:

    Error: Unknown method return type: z*
    

    What's wrong ?

    Thx again for reading this post

    Gojir4G 1 Reply Last reply
    0
    • S skylendar

      Hi there and thx for reading and answering this post in you can.

      I shouldn't ask you that because it worked before but doesn't now. Why ????

      I declare 2 C++ classes:

      class z: public QObject
      {
          Q_OBJECT
       public:
          Q_INVOKABLE z() {}
      };
      
      class test: public QObject
      {
          Q_OBJECT
       public:
          Q_INVOKABLE test() {}
      public slots:
          z* get() { auto x = new z; return x; }
      };
      `
      

      Then the corresponding declarations for QJSEngine:

      
      
      QJSValue ZObj = newQMetaObject(&z::staticMetaObject);
       globalObject().setProperty("z", ZObj);
       QJSValue TestObj = newQMetaObject(&test::staticMetaObject);
       globalObject().setProperty("test", TestObj);
      

      so far so good, but when I run a javascript script such as :

      var z = new test()
      var a = z.get()
      

      QJSEngine returns:

      Error: Unknown method return type: z*
      

      What's wrong ?

      Thx again for reading this post

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      @skylendar hi,

      Can I ask why are you declaring the constructor of z with Q_INVOKABLE and creating an object in the JS engine ?

      QJSValue ZObj = newQMetaObject(&z::staticMetaObject);
       globalObject().setProperty("z", ZObj);
      

      This should not be necessary as you don't need to create the object from Javascript.

      Then the function Test::get()must return a Javascript object wrapping instance of z:

      QJSValue get() {
          auto x = new z;
          return jsEngine->newQObject(x);
          /* or return jsEngine->newQObject(new z);*/
      }
      

      Note that by doing this you avoid memory leak of x because the Javascript engine take ownershipt of the object

      Are you sure you want to create a new instance of z each time you call get() from Javascript ? I mean, this is more a create() function :). Or do you want to "share" an object your class Test. Something like that in javascript:

      var z = new test()
      var a = z.x
      z.x.someFunction()
      
      1 Reply Last reply
      1
      • S Offline
        S Offline
        skylendar
        wrote on last edited by
        #3

        Ok, first of, thx for answering.

        Q: can I ask why are you declaring the constructor of z with Q_INVOKABLE and creating an object in the JS engine ?
        A: I use Q_INVOKABLE because it was suggested in the doc.

        Q: Are you sure you want to create a new instance of z each time you call get() from Javascript ?

        A: Don't worry, it was just a simple ad-hoc code for this post, not the real code.

        but ok, you answered my question with newObject(). I would have preferred something more straightforward.

        Thank you again.

        Gojir4G 1 Reply Last reply
        0
        • S skylendar

          Ok, first of, thx for answering.

          Q: can I ask why are you declaring the constructor of z with Q_INVOKABLE and creating an object in the JS engine ?
          A: I use Q_INVOKABLE because it was suggested in the doc.

          Q: Are you sure you want to create a new instance of z each time you call get() from Javascript ?

          A: Don't worry, it was just a simple ad-hoc code for this post, not the real code.

          but ok, you answered my question with newObject(). I would have preferred something more straightforward.

          Thank you again.

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by
          #4

          @skylendar said in glitch in javascript bindings:

          A: I use Q_INVOKABLE because it was suggested in the doc.

          Note Q_INVOKABLE is necessary only if you need to call the function from Javascript. In your code sample, this makes constructor of z available from JS, which is seemingly not what you want.

          var x = new z()  
          
          1 Reply Last reply
          1
          • S Offline
            S Offline
            skylendar
            wrote on last edited by
            #5

            Ooops, I spoke too fast. Actually, nothing works :-(

            In my case Q_INVOKABLE is needed. Also, using newQObject() doesn't change anything, since the method and properties aren't available too.

            S Gojir4G 2 Replies Last reply
            0
            • S skylendar

              Ooops, I spoke too fast. Actually, nothing works :-(

              In my case Q_INVOKABLE is needed. Also, using newQObject() doesn't change anything, since the method and properties aren't available too.

              S Offline
              S Offline
              skylendar
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • S skylendar

                Ooops, I spoke too fast. Actually, nothing works :-(

                In my case Q_INVOKABLE is needed. Also, using newQObject() doesn't change anything, since the method and properties aren't available too.

                Gojir4G Offline
                Gojir4G Offline
                Gojir4
                wrote on last edited by
                #7

                @skylendar Sorry, I miss your last message.
                Any improvements here ?

                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