[Interest] Exposing types to the QJSEngine in Qt 5.5

Sandro Andrade sandroandrade at kde.org
Mon Aug 24 06:53:38 CEST 2015


On Fri, Aug 7, 2015 at 2:23 PM, Jaroslaw Staniek <staniek at kde.org> wrote:
> Hi Again,
> No thoghts about the matter?
>
> >From https://wiki.qt.io/New_Features_in_Qt_5.5:
> "You can now have Q_PROPERTY and Q_INVOKABLE within a Q_GADGET [...]"
>
> But there's no QJSEngine::newQGadget() so is my only solution is the
> pre-QtScript glue code like this heavier class?
>
> // exposes RealObject::text() getter
> class Wrapper : public QObject
> {
> Q_OBJECT
> public:
>     Wrapper(RealObject *parent) : m_real(parent) {}
>     Q_INVOKABLE QString text() const { return m_real->text(); }
>
> private:
>     RealObject m_real;
> };
>
> // ...
> Wrapper *w = ...
> engine.globalObject().setProperty("element", engine.newQObject(w));

It seems that:

QJSEngine engine;
engine.globalObject().setProperty("element",
eng.toScriptValue<YourGadgetType>(yourGadget));
qDebug() << "Eval:" << engine.evaluate("your expression").toString();

does the trick!

Sandro

>
>
> On 9 July 2015 at 10:26, Jaroslaw Staniek <staniek at kde.org> wrote:
>> Hi,
>> Big thanks to Simon and others for time spent on exposing types to the
>> qml/js engines.
>> And to Arend van Beelen for input.
>>
>> The story continues. In an app I am suppoting .js scripts, not .qml.
>> Do I understand it well that with using QJSEngine (not QQmlEngine) for
>> value types I can use code like "new Actor()" in my scripts? (Actor is
>> a qgadget based
>> http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#value-types)
>>
>> After upgrading to 5.5 I am still getting "ReferenceError: Actor is not defined"
>>
>> Adding qRegisterMetaType<Actor>() prior to engine instantiation does not help.
>> How does the QJSEngine::toScriptValue() help?
>> I'd like to know more than the explanation at
>> https://bugreports.qt.io/browse/QTBUG-29769 or
>> https://codereview.qt-project.org/#/c/100347/ :)
>> I see that autotests in the Simon's patch only cover qml scripts, so I
>> am bit afraid what I'll hear :)
>>
>> I feel the documentation about exposing constructors is a bit lacking.
>> There's a "Making a QObject Available to the Script Engine"
>> [http://doc.qt.io/qt-5/qtjavascript.html] but it's for instances; it
>> would be useful to also cover an 'official' way of exposing types.
>> Sometimes it's good to read what's not supported in addition to what
>> is supported. Especially it would be welcome for people coming from
>> the QtScript world.
>>
>> --
>> regards, Jaroslaw Staniek
>>
>> KDE:
>> : A world-wide network of software engineers, artists, writers, translators
>> : and facilitators committed to Free Software development - http://kde.org
>> Calligra Suite:
>> : A graphic art and office suite - http://calligra.org
>> Kexi:
>> : A visual database apps builder - http://calligra.org/kexi
>> Qt Certified Specialist:
>> : http://www.linkedin.com/in/jstaniek
>
>
>
> --
> regards, Jaroslaw Staniek
>
> KDE:
> : A world-wide network of software engineers, artists, writers, translators
> : and facilitators committed to Free Software development - http://kde.org
> Calligra Suite:
> : A graphic art and office suite - http://calligra.org
> Kexi:
> : A visual database apps builder - http://calligra.org/kexi
> Qt Certified Specialist:
> : http://www.linkedin.com/in/jstaniek
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest



More information about the Interest mailing list