[Interest] Adding existing QObjects to QJSEngine

Boudewijn Rempt boud at valdyas.org
Thu Apr 21 12:29:05 CEST 2016


I want to make Krita scriptable, maybe with QJSengine. I'm trying to give
the js engine access to the existing objects in Krita: the images, documents,
windows etc.

>From the documentation, I though I would have to use QJSEngine.newQObject, but
that takes ownership and deletes the objects as soon as the engine goes out
of scope.

I thought it would work like the way one can register existing objects with
registerContextProperty()...


Here is what I'm trying to do:

{
     QString script = m_scriptEditor->ui->txtScript->document()->toPlainText();
     if (script.isEmpty()) return;

     QJSEngine engine;
     engine.installExtensions(QJSEngine::AllExtensions);

     if (m_canvas) {
         QJSValue canvas = engine.newQObject(dynamic_cast<KisCanvas2*>(m_canvas));
         engine.globalObject().setProperty("currentCanvas", canvas);
     }

     QJSValue part = engine.newQObject(KisPart::instance());
     engine.globalObject().setProperty("KisPart", part);

     QString output = engine.evaluate(script).toString();
     m_scriptEditor->ui->txtOutput->document()->setPlainText(output);
}

-- 
Boudewijn Rempt | http://www.krita.org, http://www.valdyas.org



More information about the Interest mailing list