[Interest] Import C++ types to Javascript

Jérôme Godbout jerome at bodycad.com
Tue Dec 1 23:10:47 CET 2015


First, try removing the .pragma library this make the script load before
any engine I think and cannot use any Qml import at least never got it to
work properly to create or import anything with it, you can have object
pass as arg to you to do so. Once you got it working try to put it back to
see if it cause you some pain (in that case it is, just a tips to
understand it's effect).

Second the Haser is not asingleton nor an instance, so you cannot call the
slot or Q_INVOKABLE on it. Use Qt.createComponent()
and Qt.createQmlObject() to create an object instance.

for more info on javascript to create qml object see:
http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html

Ex (you need a parent:
function hash(str, parentItem)
{
  var hasherObj = Qt.createQmlObject('import Haser 1.0; Hash {}',
parentItem, 'MyDynamicHashObject');
  // Note the last arg above is only for tracing if an error occure
  var rv = hasherObj.sha1(str);
  hasherObj.destroy();
  return rv;
}


Note: In your example, the sha1 function doesn't require any property of
the Has Object, you could put that static in C++. Also your Hash class
could be a C++ singleton in your example. Here's a small example:

replace hash by a singleton, which will be created when access in import
in your *main.cpp:*

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include "hash.h"

static QJSValue mySingletonProvider(QQmlEngine *engine, QJSEngine
*scriptEngine)
{
// TODO make this engine and script engine dependant
    return new Hash();
}

int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
//qmlRegisterType<Hash>("Hasher", 1, 0, "Hash");
qmlRegisterSingletonType<Hash>("Hasher", 1, 0, "Hash", mySingletonProvider);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

return app.exec();
}

*in Hash.h:*
make your function public static and Q_INVOKABLE (can remove the slot):

static Q_INVOKABLE QString sha1(QString str);


*in app.js:*

//.pragma library
.import Hasher 1.0 as Hasher

function hash(str) {
return Hasher.sha1(str);
}

You could also use it in Qml directly into a binding that way:
property string myHash: Hash.sha1(MyOtherObj.myString)

Hope this clarify thing a bit.

Jerome

On Tue, Dec 1, 2015 at 10:14 AM, Jason H <jhihn at gmx.com> wrote:

> Here's a sample project. TypeError on line 5 of app.js
>
>
> *Sent:* Tuesday, December 01, 2015 at 9:25 AM
>
> *From:* "Jason H" <jhihn at gmx.com>
> *To:* "Jason H" <jhihn at gmx.com>
> *Cc:* "Jérôme Godbout" <jerome at bodycad.com>, interest <
> interest at qt-project.org>
> *Subject:* Re: [Interest] Import C++ types to Javascript
> I'm still looking for help on how to use QObject slots in JS, not as
> singleton. I'm still getting that parse error.
>
> Are there any examples of QObjects being used in JS files (NOT QML! --
> .import vs import )
>
>
>
> *Sent:* Sunday, November 29, 2015 at 12:39 AM
> *From:* "Jason H" <jhihn at gmx.com>
> *To:* "Jason H" <jhihn at gmx.com>
> *Cc:* "Jérôme Godbout" <jerome at bodycad.com>, interest <
> interest at qt-project.org>
> *Subject:* Re: [Interest] Import C++ types to Javascript
> Also note that I'm talking about Javascript, not QML.
> I'm not sure if it matters, but this is me trying to use a C++ class in a
> JS file, which is then imported into QML.
>
>
> *Sent:* Sunday, November 29, 2015 at 12:33 AM
> *From:* "Jason H" <jhihn at gmx.com>
> *To:* "Jason H" <jhihn at gmx.com>
> *Cc:* "Jérôme Godbout" <jerome at bodycad.com>, interest <
> interest at qt-project.org>
> *Subject:* Re: [Interest] Import C++ types to Javascript
> To add to that: I am currently doing:
> var hash = new MyLib.Hash(); //  Parse Error: TypeError: Type error
> var result = hash.sha1('...')
> Which should work as I understand it, without a singleton. But I get the
> error above.
>
> Many thanks.
>
> *Sent:* Saturday, November 28, 2015 at 11:58 PM
> *From:* "Jason H" <jhihn at gmx.com>
> *To:* "Jérôme Godbout" <jerome at bodycad.com>
> *Cc:* interest <interest at qt-project.org>
> *Subject:* Re: [Interest] Import C++ types to Javascript
> Thanks I;ll give that a shot, but what about a non-singleton? I think my
> code should be working now,  and I doon't know why it isn't. How to I make
> an instance?
>
>
>
> *Sent:* Saturday, November 28, 2015 at 7:03 PM
> *From:* "Jérôme Godbout" <jerome at bodycad.com>
> *To:* jhihn at gmx.com
> *Cc:* interest <interest at qt-project.org>
> *Subject:* Re: [Interest] Import C++ types to Javascript
>
> 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
>
>
>
> _______________________________________________ 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/20151201/442f81bb/attachment.html>


More information about the Interest mailing list