PDA

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



AbinaThomas
5th September 2012, 10:47
Hi,

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...

spirit
5th September 2012, 13: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).

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

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


QDeclarativeView *view = new QDeclarativeView();
view->setSource(QUrl::fromLocalFile("base.qml"));
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();
}
Qmlview->show();


but I'm not getting the expected output.
My Output is :
deleteLater()
_q_reregisterTimers(void*)
updateMicroFocus()
doUpdate()

My QML file is :


import QtQuick 1.1
import File 1.1

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


How i get the "onUpdateBaseQml" slot function only?

spirit
6th September 2012, 09: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?

AbinaThomas
6th September 2012, 09: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.

spirit
6th September 2012, 09: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.