[Interest] QML objects and QMetaMethod

Vlad Stelmahovsky vladstelmahovsky at gmail.com
Mon Oct 8 06:29:03 CEST 2018


weird. it would be nice to see whole code snippet if possible

On 10/7/18 11:18 PM, Jean-Michaël Celerier wrote:
> It also works if I remove Q_OBJECT from my C++ type:
>
>
>     namespace lib {
>       class MyObject : public QObject {  };
>     }
>
>     qmlRegisterType<lib::MyObject>("MyLib", 1, 0, "MyObject");
>
> works as expected, but if I add Q_OBJECT in
>
>       class MyObject : public QObject {  };
>
> then it does not work anymore
>
>
> -------
> Jean-Michaël Celerier
> http://www.jcelerier.name
>
>
> On Sun, Oct 7, 2018 at 11:07 PM Jean-Michaël Celerier 
> <jeanmichael.celerier at gmail.com 
> <mailto:jeanmichael.celerier at gmail.com>> wrote:
>
>     > well, the problem is that createTree() does not registered in
>     your QObject-derived class.
>
>     but why does it work in the following case then ?
>
>         QtObject {
>           function createTree() { }
>         }
>
>
>     -------
>     Jean-Michaël Celerier
>     http://www.jcelerier.name
>
>
>     On Sun, Oct 7, 2018 at 1:00 PM Vlad Stelmahovsky
>     <vladstelmahovsky at gmail.com <mailto:vladstelmahovsky at gmail.com>>
>     wrote:
>
>         well, the problem is that createTree() does not registered in
>         your QObject-derived class. its a part of instantiated object
>         ant metasystem have no clue about it
>
>         I think you need to register it 1st in header somehow
>
>         On 10/7/18 12:41 PM, Jean-Michaël Celerier wrote:
>>         Yes, that's what I'm doing, but I 'm typing the code in the
>>         mail - sorry for the typo.
>>
>>         Here's something even more fundamental : the following prints
>>         all the functions if my type is a QtObject descendant, but it
>>         does not if my type is a MyLib.MyObject descendant
>>
>>             for(int i = 0; i < obj->metaObject()->methodCount(); i++) {
>>               qDebug() << obj->metaObject()->method(i).name();
>>             }
>>
>>
>>         -------
>>         Jean-Michaël Celerier
>>         http://www.jcelerier.name
>>
>>
>>         On Sun, Oct 7, 2018 at 12:39 PM Vlad Stelmahovsky
>>         <vladstelmahovsky at gmail.com
>>         <mailto:vladstelmahovsky at gmail.com>> wrote:
>>
>>             QMetaMethod::invokeMethod(obj, "createTree");
>>
>>             On 10/7/18 12:34 PM, Jean-Michaël Celerier wrote:
>>>             Hello,
>>>             I have the following code :
>>>
>>>             Foo.qml :
>>>
>>>                 MyLib.MyObject {
>>>                   function createTree() { /* stuff */ }
>>>                   property string host: "ws://whatever.com
>>>             <http://whatever.com>"
>>>                 }
>>>
>>>             cpp :
>>>
>>>                 namespace lib {
>>>                   class MyObject : public QObject { Q_OBJECT };
>>>                 }
>>>                 ...
>>>                 QQmlComponent c(engine);
>>>                 c.setData("content of Foo.qml", QUrl());
>>>
>>>                 auto obj = c.createObject();
>>>                 QString host = QQmlProperty(obj, "host").read();
>>>             QMetaMethod::invokeMethod("createTree");
>>>
>>>             Reading the "host" property works fine so the QML object
>>>             is correctly instantiated. But the invokeMethod call
>>>             gives me a warning :
>>>
>>>                 QMetaObject::invokeMethod: No such method
>>>             'lib::MyObject::createTree()'
>>>
>>>             The weird thing being: it works fine if I replace
>>>             MyLib.MyObject with QtObject on the QML side.
>>>             Of course I want to use some additional methods present
>>>             in MyObject so this is not a solution.
>>>
>>>             Anyone knows what is going on here ?
>>>
>>>             Thanks,
>>>             Jean-Michaël Celerier
>>>
>>>
>>>
>>>
>>>
>>>             -------
>>>             Jean-Michaël Celerier
>>>             http://www.jcelerier.name
>>>
>>>             _______________________________________________
>>>             Interest mailing list
>>>             Interest at qt-project.org  <mailto:Interest at qt-project.org>
>>>             http://lists.qt-project.org/mailman/listinfo/interest
>>             _______________________________________________
>>             Interest mailing list
>>             Interest at qt-project.org <mailto:Interest at qt-project.org>
>>             http://lists.qt-project.org/mailman/listinfo/interest
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20181008/fbd935eb/attachment.html>


More information about the Interest mailing list