Results 1 to 3 of 3

Thread: QJSEngine: Q_INVOKABLE method that returns pointer to subclass of QObject

  1. #1
    Join Date
    Aug 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QJSEngine: Q_INVOKABLE method that returns pointer to subclass of QObject

    Suppose I have some class that derives from QObject:

    Qt Code:
    1. class MyTest : public QObject {
    2. Q_OBJECT
    3. // ...
    4. };
    To copy to clipboard, switch view to plain text mode 

    And I want to return pointer to this class from Q_INVOKABLE function, like this:
    Qt Code:
    1. Q_INVOKABLE MyTest *createMyTest(){
    2. return new MyTest();
    3. }
    To copy to clipboard, switch view to plain text mode 
    However, when I try to invoke this method from the script in QJSEngine, I get the following error:
    Qt Code:
    1. Error: Unknown method return type: MyTest*
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. Q_DECLARE_METATYPE(MyTest*)
    To copy to clipboard, switch view to plain text mode 
    But this didn't help.
    Thanks in advance.

  2. #2
    Join Date
    Aug 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QJSEngine: Q_INVOKABLE method that returns pointer to subclass of QObject

    This helped:

    Qt Code:
    1. qmlRegisterType<MyTest>();
    To copy to clipboard, switch view to plain text mode 

    However, documentation of QJSEngine is far from perfect: it's really hard to catch (at least for me) interrelation between QML and QJSEngine. What do they have in common, and where do they differ.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QJSEngine: Q_INVOKABLE method that returns pointer to subclass of QObject

    In your case it might even be sufficient to use qmlRegisterUncreatableType().

    Though the main question is if you really need to use the function from C++ and script?

    Cheers,
    _

Similar Threads

  1. QJSEngine call method with variable arguments number
    By GuillaumeG in forum Qt Programming
    Replies: 3
    Last Post: 2nd April 2015, 08:44
  2. Replies: 9
    Last Post: 5th July 2012, 22:48
  3. Problem with QObject subclass
    By anoraxis in forum Qt Programming
    Replies: 1
    Last Post: 14th February 2012, 18:06
  4. Replies: 33
    Last Post: 2nd December 2010, 16:47
  5. QObject subclass in a QList
    By plan_rich in forum Newbie
    Replies: 3
    Last Post: 21st September 2010, 15:51

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.