[Interest] Import C++ types to Javascript

Jérôme Godbout jerome at bodycad.com
Sun Nov 29 01:03:59 CET 2015


For cpp you can use http://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterSingletonType

put your function with Q_INVOKABLE or make it a slot.

Add your class to qml module.

Then you can call the singleton class function by importing your module.

MysingletonClass.myQInvokablefct()


> On Nov 28, 2015, at 6:15 PM, jhihn at gmx.com wrote:
> 
> This is new territory for me.  Could you tell me what i need to do for both?  In this case I can make a singleton,  but in the future maybe I can't.  
> 
> Many thanks. 
> 
> -----Original message-----
> Sent: Saturday, 28 November 2015 at 02:21:06
> From: "Jérôme Godbout" <jerome at bodycad.com>
> To: "Jason H" <jhihn at gmx.com>
> Subject: Re: [Interest] Import C++ types to Javascript
> If your object is not a singleton you will need to create an instance to call the function on that object. I suggestion you keep your staticfunction  call into a singleton. So you don't need an instance.
> 
>> On Nov 27, 2015, at 8:11 PM, Jason H <jhihn at gmx.com> wrote:
>> 
>> I just checked, it is a slot. I just feel like I'm missing something. Do I need to make a instance?
>> var instance = new MyLib.Hash() ?
>> var hash = instance.sha1("data")
>> 
>> 
>>> Sent: Friday, November 27, 2015 at 7:50 PM
>>> From: "Jérôme Godbout" <jerome at bodycad.com>
>>> To: "Jason H" <jhihn at gmx.com>
>>> Cc: interest <interest at qt-project.org>
>>> Subject: Re: [Interest] Import C++ types to Javascript
>>> 
>>> Make sure your function is Q_INVOKABLE or is a slot. also take care .pragme library is not possible with javascript and object from c++ access
>>> 
>>>> On Nov 27, 2015, at 7:43 PM, Jason H <jhihn at gmx.com> wrote:
>>>> 
>>>> Per http://doc.qt.io/qt-5/qtqml-javascript-imports.html
>>>> 
>>>> I am trying to in a JS file, import a class I made and exposed to QML via
>>>> 
>>>> hash.h:
>>>> class Hash :public QObject {
>>>> Q_OBJECT
>>>> ...
>>>> QString sha1(QString str);
>>>> }
>>>> 
>>>> main(): 
>>>> qmlRegisterType<Hash>("MyLib", 1, 0, "Hash");
>>>> 
>>>> myfile.js:
>>>> .import MyLib 1.0 as MyLib
>>>> 
>>>> function myFunc(obj) {
>>>>  return MyLib.Hash.sha1(JSON.stringify(obj));
>>>> }
>>>> 
>>>> but what I get when I call it is:
>>>> qml: Parse Error: TypeError: Property 'sha1' of object [object Object] is not a function
>>>> 
>>>> What am I doing wrong?
>>>> JSON.Stringify-ing the MyLib or MyLib.Hash returns {}
>>>> 
>>>> 
>>>> 
>>>> _______________________________________________
>>>> Interest mailing list
>>>> 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/20151128/908f62bd/attachment.html>


More information about the Interest mailing list