Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
tyler_tu
Posts: 26
Registered: ‎06-17-2013
My Device: Z10
My Carrier: Bell
Accepted Solution

qml doesn't recognize the custom object from signal's parameters.

Here is my sample code to test passing a custom object from c++ to qml.

 

My custom object:

class TestObject{

public:

    QString testString;

    TestObject() { testString = "hello";}

};

...

signals:

void testError(TestObject* testObject);

 

I declare it and send the singal from c++

Q_DECLARE_METATYPE(TestObject);

...

qRegisterMetaType<TestObject>("TestObject");

...

 

emit testError(newTestObject());

 

and expect to receive the onTestError with parameter of testObject

 

It compiled and can receive the onTestError slot in qml, however it generates a runtime error of this

 

QMetaProperty::read: Unable to handle unregistered datatype 'TestObject*' for property 'QDeclarativeBoundSignalParameters::testObject'

asset:///testSreen.qml:123: Error: Unknown method parameter type: TestObject*

 

I did a couple of hours search in the forum but didn't find any solution.  Hope other developers who have the experience on the similar issue may help out.

 

Thanks,

 

Tyler

Please use plain text.
Developer
Zmey
Posts: 1,507
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: qml doesn't recognize the custom object from signal's parameters.

[ Edited ]

Hi!

emit testError(new TestObject());

This will work, but will leak memory because TestObject will be never destroyed. You'll have to delete it manually.

Pointer type has to be registered separately. In most cases it's sufficient to register only the pointer type:

Q_DECLARE_METATYPE(TestObject *);

 

If you don't plan to create instances of this class in QML, qRegisterMetaType is not needed.

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Contributor
tyler_tu
Posts: 26
Registered: ‎06-17-2013
My Device: Z10
My Carrier: Bell

Re: qml doesn't recognize the custom object from signal's parameters.

Thanks Andrey,

 

I changed it to Q_DECLARE_METATYPE(TestObject *); but still got the same run time error.

 

The code I showed is actually for narrowing down the issue.  The real design is to have a c++ based customControl in the qml and I plan to receive the signal and pass the testObject to my c++ customControl and release it there.

Please use plain text.
Contributor
tyler_tu
Posts: 26
Registered: ‎06-17-2013
My Device: Z10
My Carrier: Bell

Re: qml doesn't recognize the custom object from signal's parameters.

I actually resolved this by using 

 

qRegisterMetaType<TestObject*>("TestObject*");

 

I check the Q_DECLARE_METATYPE, it should did the same thing but unfortunately not working in my case.

 

Thanks Andrey for the clue. 

Please use plain text.