[Interest] Replacing QScript with QML

Jérôme Godbout jerome at bodycad.com
Wed Feb 15 19:55:33 CET 2017


You could create a singelton Factory that will create the objects into
javascripts and wrap the whole creation into a simple functions, you can
also make a map from object name to the actual Qml Component. Just make
sure you give the object the proper parenting and ownership and delete them
properly.

// MyFactorySingelton.qml
pragma Singleton
import QtQuick 2.3

QmlObject
{
id: component
readonly property int objUno: 0
readonly property int objTwo: 1

readonly property var objType:
[
 'objUno'
, 'objTwo'
]
readonly property var objComponent: component.revaluate.concat(
[
 Qt.createComponent('ObjectUno.qml', Component.PreferSynchronous, component)
, Qt.createComponent('ObjectTwo.qml', Component.PreferSynchronous,
component)
])
function createByName(name, parent, initMap) // ex: createByName('objUno',
myParent, { 'width': 500, 'height': 200 })
{
return createById(component[component.objType[name]], parent, initMap);
}
function createById(obj_index, parent, initMap) // ex: create(1, myParent,
{ 'width': 500, 'height': 200 })
{
return objComponent[obj_index].create(parent, );
}
function create(comp, parent, initMap)
{
return comp.createObject(parent, initMap);
}
}

you can use it into your javascript
*.import MyFactoryModule 0.1 as MyFactory*

or qml script easily after that
*import MyFactoryModule 0.1 *
*MyFactorySingelton.createByName('objUno')*

If you are looking at only kicking some script, Component.onCompleted or
use the WorkerScript to kick javascript only scripting:
http://doc.qt.io/qt-5/qml-workerscript.html


[image: bodycad] <https://www.bodycad.com/>
Jerome Godbout
Software Developer
2035 rue du Haut-Bord, Québec, QC, Canada. G1N 4R7
T:  +1 418 527-1388
E: jerome at bodycad.com
www.bodycad.com

The contents of this email message and any attachments are intended solely
for the addressee(s) and may contain confidential and/or privileged
information and may be legally protected from disclosure. If you are not
the intended recipient of this message or their agent, or if this message
has been addressed to you in error, please immediately alert the sender by
reply email and then delete this message and any attachments. If you are
not the intended recipient, you are hereby notified that any use,
dissemination, copying, or storage of this message or its attachments is
strictly prohibited.

Le contenu de ce message et les pièces jointes sont destinés uniquement
pour le(s) destinataire(s) et peuvent contenir des informations
confidentielles et / ou privilégiées qui peuvent être protégées légalement
contre toute divulgation. Si vous n'êtes pas le destinataire de ce message
ou son agent, ou si ce message vous a été adressé par erreur, s’il vous
plaît avertir immédiatement l'expéditeur par courriel de réponse, puis
supprimer ce message et les pièces jointes. Si vous n'êtes pas le
destinataire prévu, vous êtes par la présente informé que toute
utilisation, diffusion, copie, ou stockage de ce message ou de ses pièces
jointes est strictement interdit.

On Wed, Feb 15, 2017 at 1:30 PM, Ian Geiser <geiseri at geekcentral.pub> wrote:

>  ---- 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!
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170215/4e2fd3d6/attachment.html>


More information about the Interest mailing list