[Interest] Questions about QML

Sze Howe Koh szehowe.koh at gmail.com
Wed Oct 1 04:32:18 CEST 2014


Hi,

On 1 October 2014 09:05, Oleg Shparber <trollixx at gmail.com> wrote:
> Hi,
>
> I am working on a QML plugin which should provide a set of "modules" with
> functionality similar to some of built-in Node.js modules. The requirement
> also is that third-party JS libraries created for Node.js should also work
> out of the box. While much of the work is straightforward, there are still
> some questions I cannot find solutions for.
>
> 1. For some reason when I am setting a property on QJSEngine::globalObject()
> in my plugin it does not become available in QML. Example:
>
> static QObject *singletontype_provider(QQmlEngine *engine, QJSEngine
> *scriptEngine)
> {
>     engine->globalObject().setProperty("prop1", "aaa");
>     scriptEngine->globalObject().setProperty("prop2", "aaa");
>     ...
> }
>
> I've tried with both QJSEngine and QQmlEngine passed to the plugin, but
> neither works.
>
>
> 2. Can I set new properties of the global object in JS/QML? Trying to set

(1+2) That's not possible with the QML JavaScript engine. According to
"JavaScript Environment Restrictions" in
http://qt-project.org/doc/qt-5/qtqml-javascript-hostenvironment.html:

"In QML, the global object is constant - existing properties cannot be
modified or deleted, and no new properties may be created."

Side note: The JavaScript engine implemented in Qt is designed
specifically for QML code in mind. That's one of the main reasons for
dropping V8 in favour of a custom JavaScript engine.

I don't know the answer to your other questions, so I'll let others answer them.


> 3. How can I make my C++ type creatable with "new" in QML/JS? I want to
> provide several new JS types, but not QML types.
>
>
> 4. Is there a way to provide my own container type? Overloading [] with
> Q_INVOKABLE does not work. I'd like to have a custom buffer type which I can
> use as myBuffer[index].

(4) How does that differ from a regular JavaScript array?


> 5. Is QJSEngine::evaluate() the only method to execute a separate JS file?
>
> 6. Can I somehow create a new callable QJSValue using QMetaMethod or lambda
> function? It's possible to do from JS side (e. g.
> NewType.prototype.newMethod = CppObject.method; works), but I want to do it
> from C++ side.

(6) May I ask why you want to do it from the C++ side?


Regards,
Sze-Howe



More information about the Interest mailing list