PDA

View Full Version : Qml non Gui script



codeman
17th July 2014, 09:09
Hello friends,

is there any possibility to execute a qml script with a non gui root element.



Thanx in advance

anda_skoa
17th July 2014, 17:27
QQmlEngine does not have any UI dependencies, you can instantiate any type you've registered as the top element.
Only requirement for types is that they have QObject somewhere in their inheritance hierachy.

Cheers,
_

codeman
18th July 2014, 10:48
Hmm, I don´t know if I understand you correct. I think I have to explain it better:

I have wrtitten some c++ qml plugins which I can import in qml with the import directives.

But when I use my plugin:



import QtQuick 2.2
import QtQuick.Controls 1.2
import MySoft.labs.Plugs 1.0

Rectangle {
id: root

width: 300; height: 300
color: "white"

QMyPlugin{
id: oPlug
}
Component.onCompleted: {
oPlug.execute();
}
}



So here I have to wrapp it with a rectangle.

So my question in detail is, is there a qml element/container for executing non gui tasks or whatever.

How can I register a type as top element?

I know that I can register a type witj qmlregistertype etc., but your suggestions sounds new to me.


Yours,

anda_skoa
18th July 2014, 13:42
If your type is in a plugin, that plugin will have registered the type so you don't have to.

How are you loading your QML?

Cheers,
_

codeman
18th July 2014, 16:10
Hello,

the registering of the plugin is done in here:



class QMyPlugPlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "com.mysoft.Qt.QMyPlugin")

public:
virtual void registerTypes(const char *uri)
{
Q_ASSERT(QLatin1String(uri) == QLatin1String("MySoft.labs.Plugs"));
qmlRegisterType<QMyPlugin>(uri,1,0,"QMyPlugin");
}
};


and I start the qml file with the qml.exe.

anda_skoa
19th July 2014, 13:28
the registering of the plugin is done in here:

Right. The plugin should have taken care of the registring.



and I start the qml file with the qml.exe.

Ah, I assumed you were loading that into an application of your own.

Still, the QQmlEngine used by the "qml" runtime should still be able to runa non UI application, it even has an application type that creates a QCoreApplication.

The following non-UI code runs fine for me


import QtQml 2.0

Timer {
id: timer
interval: 500

onTriggered: console.log("timeout");

Component.onCompleted: timer.start()
}


Cheers,
_

codeman
21st July 2014, 10:11
ok.

You mean I could embedd it this way:



import QtQml 2.0
import MySoft.labs.Plugs 1.0

Timer {
id: timer
interval: 500
QMyPlugin{
id: oPlug
}
onTriggered: {
oPlug.execute();
console.log("timeout and execute");
}

Component.onCompleted: timer.start()
}

anda_skoa
21st July 2014, 11:32
You mean I could embedd it this way:

No.
I mean that TImer, being a non-UI type, works as expected. Even with -apptype core

Cheers,
_