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 <[email protected]> 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" <[email protected]> > *To:* "Jason H" <[email protected]> > *Cc:* "Jérôme Godbout" <[email protected]>, interest < > [email protected]> > *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" <[email protected]> > *To:* "Jason H" <[email protected]> > *Cc:* "Jérôme Godbout" <[email protected]>, interest < > [email protected]> > *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" <[email protected]> > *To:* "Jason H" <[email protected]> > *Cc:* "Jérôme Godbout" <[email protected]>, interest < > [email protected]> > *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" <[email protected]> > *To:* "Jérôme Godbout" <[email protected]> > *Cc:* interest <[email protected]> > *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" <[email protected]> > *To:* [email protected] > *Cc:* interest <[email protected]> > *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, [email protected] 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" <[email protected]> > To: "Jason H" <[email protected]> > 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 <[email protected]> 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" <[email protected]> > > To: "Jason H" <[email protected]> > > Cc: interest <[email protected]> > > 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 <[email protected]> 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 > > [email protected] > > http://lists.qt-project.org/mailman/listinfo/interest > > > > _______________________________________________ Interest mailing list > [email protected] > http://lists.qt-project.org/mailman/listinfo/interest >
_______________________________________________ Interest mailing list [email protected] http://lists.qt-project.org/mailman/listinfo/interest
