Results 1 to 7 of 7

Thread: How to parse QML file from QT C++

  1. #1
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default How to parse QML file from QT C++

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

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to parse QML file from QT C++

    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).
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following 2 users say thank you to spirit for this useful post:

    AbinaThomas (6th September 2012), Raycho Raykov (18th May 2013)

  4. #3
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to parse QML file from QT C++

    Thank you very much for your replay. "QMetaObject" and "QMetaMethod" are new terms for me. I will refer its tutorial.

  5. #4
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to parse QML file from QT C++

    I tried with QMetaObject. My sample code is
    Qt Code:
    1. QDeclarativeView *view = new QDeclarativeView();
    2. view->setSource(QUrl::fromLocalFile("base.qml"));
    3. Qmlview = view;
    4. QObject *item = view->rootObject();
    5. for(int i=0; i<item->metaObject()->methodCount(); i++)
    6. {
    7. QMetaMethod meta = item->metaObject()->method( i );
    8. if( meta.methodType() == QMetaMethod::Slot )
    9. qDebug() << meta.signature();
    10. }
    11. Qmlview->show();
    To copy to clipboard, switch view to plain text mode 

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


    My QML file is :
    Qt Code:
    1. import QtQuick 1.1
    2. import File 1.1
    3.  
    4. Rectangle {
    5. width: 400
    6. height: 400
    7. color: "#d5b9b9"
    8. TextUpdate
    9. {
    10. id : linkFile
    11. }
    12. Connections
    13. {
    14. target: linkFile
    15. onUpdateBaseQml:
    16. {
    17. console.log("Got event")
    18. timeText.text = time
    19. speedText.text = speed
    20. rpmText.text = rpm
    21. fuelText.text = fuel
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    How i get the "onUpdateBaseQml" slot function only?

  6. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to parse QML file from QT C++

    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?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #6
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to parse QML file from QT C++

    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.

  8. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to parse QML file from QT C++

    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.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. The following user says thank you to spirit for this useful post:

    AbinaThomas (7th September 2012)

Similar Threads

  1. Parse huge XML file
    By juracist in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2012, 01:54
  2. Parse xml file using QStreamReader
    By Gary.zhang in forum Qt Programming
    Replies: 0
    Last Post: 10th January 2011, 04:00
  3. Replies: 13
    Last Post: 21st June 2006, 21:22
  4. How to parse this XML file ?
    By probine in forum Qt Programming
    Replies: 7
    Last Post: 4th April 2006, 09:05
  5. Parse a pickle file
    By karye in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2006, 17:02

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.