[Qt-qml] Function in QScriptValue can only be called for a short time

Girish Ramakrishnan girish at forwardbias.in
Thu Feb 10 19:07:31 CET 2011


Hi Ville,

On Thu, Feb 10, 2011 at 6:06 PM, Ville M. Vainio <vivainio at gmail.com> wrote:
> I am using QScriptValue to store a javascript function. I have a C++
> method that stores it aside for later use:
>
>
> void Deferred::onRes(const QScriptValue &v)
> {
>    m_res = v;
>
>    //  m_res.call());  THIS WOULD WORK
> }
>
> - If I invoke call() as in the snippet above, in comments, the
> function is called alright.
> - If I invoke call() from the same QML method that called onRes, the
> call works as well
>
> - HOWEVER, if I invoke it even later (on some signal handler), the
> function doesn't get called.
>

This should work.

> m_res.toString() shows the correct function body in all cases. It's as
> if the link to script engine got broken somehow?
>

Does evaluating things in the QScriptValue::engine() work?

> Any idea what "spoils" the QScriptValue in this fashion? I have tried
> to ensure that nothing gets garbage collected, so I don't think that's
> the cause.
>

As I understand, QScriptValue is refcounted, so it won't get garbage
collected as long as you keep a reference to the function. So, as Kent
asked, a small testcase would help.

Girish


More information about the Qt-qml mailing list