1

I have a subclass of a QObject which I can already create in QtScript using var x = new Test();

I made a constructor function and registered it using QScriptEngine::​newFunction and QScriptEngine::​newQMetaObject like this:

QScriptValue construct_Test(QScriptContext *context, QScriptEngine *engine)
{
    Test * ptr = new Test();
    return engine->newQObject(ptr);
}

....
QScriptValue constructor = pEngine->newFunction(construct_Test);
QScriptValue metaObject = pEngine->newQMetaObject(&Test::staticMetaObject, constructor);
pEngine->globalObject().setProperty("Test", metaObject);

I manage the object lifetime by myself (pointers to objects are stored in a static member of Test).

How can I make delete x; in QtScript delete the underlying C++ object? I know about x.deleteLater();(slot from QObject) but I can't use it.

Nazar554
  • 4,105
  • 3
  • 27
  • 38
  • I guess you could somehow create a a public slot on a QObject that would make the delete. Workaround. – user2672165 Jan 02 '15 at 19:05
  • @user2672165 You mean make a slot which does `delete this;`? – Nazar554 Jan 02 '15 at 19:16
  • `delete this` would be a nice solution, but you could also make a special memory manager object. However, then you need to supply an identifier to specify what to delete. – user2672165 Jan 02 '15 at 19:33
  • Does your object have a parent? The parent object will delete all children automatically when it is deleted. – Jay Jan 03 '15 at 16:21

0 Answers0