View Full Version : How to parse QML file from QT C++

5th September 2012, 11:47

I'm working in the back end of QML file. I wand to send some signals to QML. So I need to parse the QML file and find out which signals are handled in the QML file. Is there any way to parse a QML file from QT c++??

Thanks in advance...

5th September 2012, 14:10
Well, you can do this in other way, without parsing anything: you can traverse QML objects from C++ and check objects' metainfo.
You'll need to make a loop by QMetaMethod (using QMetaObject::methodCount and QMetaObject::method) and check if the current method is a signal (using QMetaMethod::methodType).

6th September 2012, 06:06
Thank you very much for your replay. "QMetaObject" and "QMetaMethod" are new terms for me. I will refer its tutorial.

6th September 2012, 09:57
I tried with QMetaObject. My sample code is

QDeclarativeView *view = new QDeclarativeView();
Qmlview = view;
QObject *item = view->rootObject();
for(int i=0; i<item->metaObject()->methodCount(); i++)
QMetaMethod meta = item->metaObject()->method( i );
if( meta.methodType() == QMetaMethod::Slot )
qDebug() << meta.signature();

but I'm not getting the expected output.
My Output is :

My QML file is :

import QtQuick 1.1
import File 1.1

Rectangle {
width: 400
height: 400
color: "#d5b9b9"
id : linkFile
target: linkFile
console.log("Got event")
timeText.text = time
speedText.text = speed
rpmText.text = rpm
fuelText.text = fuel

How i get the "onUpdateBaseQml" slot function only?

6th September 2012, 10:31
You need to iterate by children of the root object. In you example you are only iterating by root itself.
EDITED: And you need signals not slots, no?

6th September 2012, 10:44
How to iterate by children of the root object? Do you have any sample code??

I want the slots which are defied in the QML file.
If I send a signal named "updateBaseQml" from CPP then its SLOT in QML is "onUpdateBaseQml". an i right?
I want that function name like "onUpdateBaseQml".

thanks in advance.

6th September 2012, 10:47
That's what you wrote

...So I need to parse the QML file and find out which signals are handled in the QML file....

Anyway, use QObject::children to get a list of children for a given object.