Results 1 to 10 of 10

Thread: Query in Reading QML file

  1. #1
    Join Date
    Nov 2014
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Question Query in Reading QML file

    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

    Qt Code:
    1. //---------------------------------------------------
    2. //---------------------------------------------------
    3. //Main.qml
    4. import QtQuick 2.0
    5. Clock {
    6.  
    7. hours: 10
    8. minutes: 10
    9. Text {
    10. id: cityLabel1; font.bold: true; font.pixelSize: 14; y:200; color: "white"
    11. objectName: "RootText"
    12. }
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //---------------------------------------------------
    2. //---------------------------------------------------
    3.  
    4. //Clock.qml
    5. import QtQuick 2.0
    6.  
    7. Rectangle {
    8. id: clock
    9. width: 200; height: 200; color: "gray"
    10.  
    11. property alias city: cityLabel.text
    12. property variant hours
    13. property variant minutes
    14. property variant shift : 0
    15.  
    16. Image { id: background; source: "clock.png"; objectName: "ClockImgae1"}
    17.  
    18. Image {
    19. x: 92.5; y: 27
    20. source: "hour.png"
    21. objectName: "ClockImgae2"
    22. transform: Rotation {
    23. id: hourRotation
    24. origin.x: 7.5; origin.y: 73;
    25. angle: (clock.hours * 30) + (clock.minutes * 0.5)
    26. Behavior on angle {
    27. SpringAnimation{ spring: 2; damping: 0.2; modulus: 360 }
    28. }
    29. }
    30. }
    31.  
    32. Image {
    33. x: 93.5; y: 17
    34. source: "minute.png"
    35. objectName: "ClockImgae3"
    36. transform: Rotation {
    37. id: minuteRotation
    38. origin.x: 6.5; origin.y: 83;
    39. angle: clock.minutes * 6
    40. Behavior on angle {
    41. SpringAnimation{ spring: 2; damping: 0.2; modulus: 360 }
    42. }
    43. }
    44. }
    45.  
    46. Image {
    47. anchors.centerIn: background; source: "center.png"; objectName: "ClockImgae4"
    48. }
    49.  
    50. Text {
    51. id: cityLabel; font.bold: true; font.pixelSize: 14; y:200; color: "white"
    52. anchors.horizontalCenter: parent.horizontalCenter
    53. objectName: "ClockText"
    54. }
    55. }
    56. //---------------------------------------------------
    57. //---------------------------------------------------
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 13th November 2014 at 14:52. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Query in Reading QML file

    The children added in the base file are first in the list. The children added in the file using the type come last.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Query in Reading QML file

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Nov 2014
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Question Re: Query in Reading QML file

    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

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Query in Reading QML file

    Quote Originally Posted by harikrishnanel View Post
    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Query in Reading QML file

    Quote Originally Posted by harikrishnanel View Post
    @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,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    harikrishnanel (14th November 2014)

  8. #7
    Join Date
    Nov 2014
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Query in Reading QML file

    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

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Query in Reading QML file

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Nov 2014
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Query in Reading QML file

    Thank you for you reply.

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

    Regards,
    Hari Krishnan EL

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Query in Reading QML file

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:

    harikrishnanel (14th November 2014)

Similar Threads

  1. reading pdf file
    By kito in forum Newbie
    Replies: 8
    Last Post: 23rd June 2012, 08:17
  2. problem with using xpath to query from xml file
    By aya_lawliet in forum Qt Quick
    Replies: 0
    Last Post: 7th September 2011, 15:38
  3. Reading XML file into a data file correctly?
    By falconium in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2011, 18:55
  4. Execute a .sql file with more than one query
    By ShadowBelmolve in forum Newbie
    Replies: 6
    Last Post: 19th August 2010, 16:48
  5. How to query data by id in a text file
    By SamSong in forum Qt Programming
    Replies: 4
    Last Post: 26th May 2009, 10:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.