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. QJSEngine: Q_INVOKABLE method that returns pointer to subclass of QObject
QtWS25 Last Chance

QJSEngine: Q_INVOKABLE method that returns pointer to subclass of QObject

Scheduled Pinned Locked Moved QML and Qt Quick
qjsenginejavascriptqobject
4 Posts 2 Posters 6.0k 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.
  • D Offline
    D Offline
    dimonomid
    wrote on 17 Aug 2015, 23:02 last edited by dimonomid
    #1

    Suppose I have some class that derives from QObject:

    class MyTest : public QObject {
    Q_OBJECT
       // ... 
    };
    

    And I want to return pointer to this class from some Q_INVOKABLE method, like this:

       Q_INVOKABLE MyTest *createMyTest(){
          return new MyTest();
       }
    

    However, when I try to invoke this method from the script in QJSEngine, I get the following error:

    Error: Unknown method return type: MyTest*
    

    If I make createMyTest() to return QObject* instead of MyTest*, it works. But of course this would force me to use casts, which I'd like to avoid. So, how to make it work with MyTest*?
    Just by chance, I tried to declare metatype, as follows:

    Q_DECLARE_METATYPE(MyTest*)
    

    But this didn't help.
    Thanks in advance.

    1 Reply Last reply
    1
    • L Offline
      L Offline
      Leonardo
      wrote on 18 Aug 2015, 03:01 last edited by
      #2

      You need to register your subclass to have it available in qml.

      qmlRegisterType<MyTest>();
      
      D 1 Reply Last reply 18 Aug 2015, 07:40
      1
      • L Leonardo
        18 Aug 2015, 03:01

        You need to register your subclass to have it available in qml.

        qmlRegisterType<MyTest>();
        
        D Offline
        D Offline
        dimonomid
        wrote on 18 Aug 2015, 07:40 last edited by dimonomid
        #3

        @Leonardo, thank you, that works.

        I'm still confused by the QML and QJSEngine interrelation. Like, how large part of QML should I learn in order just to use QJSEngine.

        And, for example, how to use qml's import in JavaScript (not QML) code? Say, if I register instantiable type like this:

        qmlRegisterType<MyTest>("com.mycompany.myclasses", 1, 0, "MyTest");
        

        Does that mean that I should import com.mycompany.myclasses somehow, in order for things to work correctly? QJSEngine documentation doesn't contain things related to this.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          Leonardo
          wrote on 18 Aug 2015, 14:42 last edited by
          #4

          Hi. I'm not sure I understand what you mean, but take a look here:

          http://doc.qt.io/qt-5/qtqml-javascript-imports.html#imports-within-javascript-resources

          It might help.

          1 Reply Last reply
          0

          1/4

          17 Aug 2015, 23:02

          • 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