[Interest] Replacement for Qt Script

Ben Lau xbenlau at gmail.com
Thu Apr 21 11:21:43 CEST 2016


On 18 April 2016 at 23:38, Ian Geiser <geiseri at geekcentral.pub> wrote:

>  ---- On Mon, 18 Apr 2016 11:06:59 -0400 Jason H <jhihn at gmx.com> wrote
> ----
>  > ttps://
> forum.qt.io/topic/52306/qt-5-5-qt-script-deprecated-what-is-replacement/12
>  > >
>  > > I saw this thread, but it did not seem to offer a clear detail other
> than to use QJSEngine and QJSValue directly.  Specifically I use
> qScriptRegisterMetaType very heavy to add custom types.  I know if I am
> using QML I can use qmlRegisterType<T>(uri, 1, 0, "T") but it only makes
> the type available via the declarative components.  It also requires the
> object be a QObject which means sharing pointers.  There is also a problem
> inside of the Javascript.  It is very cumbersome to dynamically create
> these types.  I understand I am fighting QML, but my design is not as
> declarative as it is creating objects that can be passed back into C++.   I
> may be missing something though.
>  > >
>  > > As an example below with a type called MyStruct:
>  > >
>  > > ... Qt Script...
>  > >
>  > > var myStruct = new MyStruct();
>  > > myStruct.x = 10;
>  > >
>  > >  ....QML...
>  > >
>  > > var myStruct;
>  > > var component = Qt.createComponent(???);
>  > >  if (component.status == Component.Ready) {
>  > >         myStruct = component.createObject(parent, {"x": 100, "y":
> 100});
>  > > }
>  > >
>  > > What would ??? be?
>  >
>  > If MyStruct is just a JS object, then just use JS. function MyStruct(){
> return {"x": 100, "y": 100}; }; var myStruct = new MyStruct();
>  > You only need createObject when you are creating an object defined in
> C++ or dynamic object from QML.
>
> That is my rub here.  These are custom QML components that are being
> created in JS and then passed back into the C++ end of things.  The goal
> being I want to be able to write "plugins" for my app using JS instead of
> C++. So I am creating and manipulating custom types that are then used to
> manipulate and extend the C++.  I may be making this harder than it needs
> to be though.  I will give it a try with the non-gui QML components and see
> how far I get.
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>


Suggestion

1. Register a singleton QML component by C++.

2. The C++ component provides hook function  (callback in Javascript)
registration.

void MyAPI::registerHook(QJSValue callback) {
}

3. Whatever an event occur, call the hook function.

QJSValueList args;
QJSValue ret = callback.call(args);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160421/ad969b4c/attachment.html>


More information about the Interest mailing list