[Interest] Replacing QScript with QML

Ian Geiser geiseri at geekcentral.pub
Wed Feb 15 19:30:06 CET 2017


 ---- On Tue, 14 Feb 2017 11:06:36 -0500 Jason H <jhihn at gmx.com> wrote ---- 
 >  
 >  
 > > Sent: Tuesday, February 14, 2017 at 10:14 AM 
 > > From: "Ian Geiser" <geiseri at geekcentral.pub> 
 > > To: Interest at qt-project.org 
 > > Subject: [Interest] Replacing QScript with QML 
 > > 
 > >  
 > >  
 > >  
 > > Greetings, I am ported a non-gui Qt4 QScript apt to Qt5 and would like to replace the QScript component with QML.  I am struggling with quite a few rudimentary things though. 
 > >  
 > > My biggest problem is custom Javascript types:  I made everything a QObject based class, but allocating these objects in javascript is messy.  Is there a way to get close to something as simple as "var foo = new Foo()"? 
 > >  
 > > Has anyone done this before?  Are there any examples?  Maybe I am putting a square peg into a round hole here.  Any insight from the Qt guys here? 
 >  
 > https://wiki.qt.io/QML_Dynamic_Objects ? 
 > 

That is what I am wanting to avoid.  I have about 15 c++ types that are used to control the underlying system.  The biggest one is  a simple wrapper around QProcess.  So i have a plugin that wraps it and define it like so:

===
    m_engine = new QQmlEngine(this);
    addGlobalProperty("ScriptEngine", this);

    qmlRegisterType<Process>("org.geekcentral.api", 1, 0, "Process");
     
   QQmlComponent *component = new QQmlComponent(m_engine);
   component->loadUrl("myscript.qml);

   Object *scriptInstance = component->create(m_engine->rootContext());
===

Now what I really want to be able to do is something like this in the QML:

===
import QtQuick 2.0
import org.geekcentral.api 1.0


QtObject {
    id: plugin
    Component.onCompleted: {
       console.log("Loaded");
       var process = new Process(this);
       // ... set up process
       process.run();
    }
}
===

Like you said I end up with the Qt.createQmlObject(...) code, but this is messy and cumbersome to do with all my types.


The only other approach I found was like below:

===
import QtQuick 2.0
import org.geekcentral.api 1.0


QtObject {
    id: plugin
    property Process process: Process {
       Component.onCompleted: run()
   }

    Component.onCompleted: {
       console.log("Loaded");
    }
}
===

It seems a bit more verbose, so I am wondering if there is a better way to actually pull this off.

Thanks!





More information about the Interest mailing list