PDA

View Full Version : Query in Reading QML file



harikrishnanel
13th November 2014, 14:55
Hi,
I am new to QML. I am writing specific information alone from QML to XML. My main QML file is "Main.qml" and the root refers to "Clock.qml".
After loading the QML, i am fetching the children from the root "Clock".

QObjectList listOfChildren = rootObj->children();

This list contains following child and its object name are:
"ClockImgae1"
"ClockImgae2"
"ClockImgae3"
"ClockImgae4"
"ClockText"
"RootText"

But I want to get only the child object of Clock in Main.qml (i.e., object related to "RootText") and write its objectName in XML.

I didn't find a way to do that. Please help.

Thanks in Advance.

Regards,
Hari Krishnan EL



//---------------------------------------------------
//---------------------------------------------------
//Main.qml
import QtQuick 2.0
Clock {

hours: 10
minutes: 10
Text {
id: cityLabel1; font.bold: true; font.pixelSize: 14; y:200; color: "white"
objectName: "RootText"
}

}



//---------------------------------------------------
//---------------------------------------------------

//Clock.qml
import QtQuick 2.0

Rectangle {
id: clock
width: 200; height: 200; color: "gray"

property alias city: cityLabel.text
property variant hours
property variant minutes
property variant shift : 0

Image { id: background; source: "clock.png"; objectName: "ClockImgae1"}

Image {
x: 92.5; y: 27
source: "hour.png"
objectName: "ClockImgae2"
transform: Rotation {
id: hourRotation
origin.x: 7.5; origin.y: 73;
angle: (clock.hours * 30) + (clock.minutes * 0.5)
Behavior on angle {
SpringAnimation{ spring: 2; damping: 0.2; modulus: 360 }
}
}
}

Image {
x: 93.5; y: 17
source: "minute.png"
objectName: "ClockImgae3"
transform: Rotation {
id: minuteRotation
origin.x: 6.5; origin.y: 83;
angle: clock.minutes * 6
Behavior on angle {
SpringAnimation{ spring: 2; damping: 0.2; modulus: 360 }
}
}
}

Image {
anchors.centerIn: background; source: "center.png"; objectName: "ClockImgae4"
}

Text {
id: cityLabel; font.bold: true; font.pixelSize: 14; y:200; color: "white"
anchors.horizontalCenter: parent.horizontalCenter
objectName: "ClockText"
}
}
//---------------------------------------------------
//---------------------------------------------------

anda_skoa
13th November 2014, 15:55
The children added in the base file are first in the list. The children added in the file using the type come last.

Cheers,
_

wysota
13th November 2014, 16:21
You could add an "objectName" property to the Clock instance, then fetch it from C++ using findChild() and ask for its children, however "pulling" objects from QML to C++ is usually a bad idea. It is better to push an object from C++ to QML and use it there. So in your case you would push some object to QML where the script could attach some content to it (e.g. The Clock instance) that could then be processed by the exported object.

harikrishnanel
14th November 2014, 06:40
Thank you for the replies.

@wysota : QML which we are reading is from Third party/Client. So we are not sure, what object name it contains.


@anda_skoa : You have mentioned that "children added in the file using the type come last". If I am looping through the child objects of Root QML(Main.qml), how I will identify that child is from Main.qml

QObjectList listOfChildren = rootObj->children();//Root object of Main.qml
int i = 0;
for(i = 0; i < listOfChildren.count(); i++)
{
QObject* child = listOfChildren.at(i);
QString objName1 = child->objectName();
QString classType1 = child->metaObject()->className();
if()//What condition I have to give to check child added in Main.QML
{
}
}

Thanks and Regards,
Hari Krishnan.EL

wysota
14th November 2014, 08:43
Thank you for the replies.

@wysota : QML which we are reading is from Third party/Client. So we are not sure, what object name it contains.

Then I would say that what you are doing was terribly wrong. What if the object you were looking for was not in that file at all?

Could you state the ultimate goal behind what you are trying to do?

anda_skoa
14th November 2014, 09:28
@anda_skoa : You have mentioned that "children added in the file using the type come last". If I am looping through the child objects of Root QML(Main.qml), how I will identify that child is from Main.qml


There is no such thing as "from Main.qml" during runtime.

The engine parses code and creates an object tree. You access that generated tree. An object does not have a tag which piece of code lead to its creation.

If you control the definition of the base type then let it carry the information which of its base children is last, e.g. by having property that points to it or a property with the base child count or mark the last child.

Generally, since the information you are looking for is present in the code, parsing the code will be the most reliable approach.

Cheers,
_

harikrishnanel
14th November 2014, 09:58
Hi Wysota,
I am not the owner of QML files. I am getting set of QML files from client which are simliar to our example files.

My requirement is to load a QML file. Get Child objects in that QML. Check for ObjectName for each child. If ObjectName is not empty string, write the object name of the child and its class type in XML file in specific schema.

By our example converting Main.qml to Main.XML

I am loading Main.qml and getting children of rootObject "Clock". This gives me a list of child with the objectName is below order:
"ClockImgae1"
"ClockImgae2"
"ClockImgae3"
"ClockImgae4"
"ClockText"
"RootText"

In this, child object related to "RootText" is in Main.qml and others are in Clock.qml.

As per our requirment, I have to get only the childObjects of Clock in Main.qml (i.e., childObject with objectName ""RootText"") and have to ignore other childObjects(i.e., from Clock.qml). If the objectName is not empty, I will write it in XML file along with child object Class type.

My issue is, I am not finding the way to segregate the child Object of Clock in Main.qml from the child object list retrieved from rootObject.

I hope I am making it clear now. Please help.

Thanks and Regards,
Hari Krishnan.EL

wysota
14th November 2014, 11:21
In that case I agree with Kevin - parse the source file and extract the needed information manually. QML document can be simplified to a JSON string which is easy to parse. You can also use regular expressions to find elements and then extract object name from it.

harikrishnanel
14th November 2014, 11:27
Thank you for you reply.

Please provide me sample code / links for parsing. I will search from my side too.

Regards,
Hari Krishnan EL

wysota
14th November 2014, 11:33
Google is full of materials on implementing parsers, this is nothing Qt specific. Qt is also open source software, you can take QML parsing code directly from the repository and adapt it to your needs, provided the license allows it. If you decide to take the route of simplifying the document and parsing it as JSON, QJsonDocument can help you.