Pass data between QML files



  • i have a main.qml file a child.qml file, the problem is that when i register a type in the main.qml file it can't access it from child.qml, th equestion is: How I can pass data between 2 qml files? Thank you.



  • @mandruk1331 hi, can you post the code



  • @mandruk1331
    There are a couple of ways. You can write a function for the child.qml top level object and call it from main.qml. Or you can create a property for the child.qml top level object and set it from main.qml. So child.qml gets data from main.qml indirectly.



  • @Eeli-K there's not much code:

           QQmlApplicationEngine *engine = new QQmlApplicationEngine ;
          engine->rootContext()->setContextProperty("Object",&O);
          engine->load(QUrl(QLatin1String("qrc:/some_qml.qml")));


  • @mandruk1331 It was Naveen_D who asked for the code.
    Anyways, I just realized your need is more than just basic passing data between qml components, even though you asked "How I can pass data between 2 qml files?". We need the qml code, too.

    Change the name of the object to begin with lower case. With your C++ code you should be able to use the object in any qml file. What do you mean by " register a type in the main.qml file"? If you have

    O objecty();
    engine->rootContext()->setContextProperty("objectx",&O);
    

    in C++ you should be able to do for example

    objectx.someSlot()
    var x = objectx.propertyX
    

    in every qml file.
    But a C++ object is an overkill for passing data between qml components unless you need it for some specific reason, so do you already know how to do it in pure qml and is your need more than that?



  • @Eeli-K I figured out how to do it, maybe in a little bit strange way but it works, when the button is clicked I call a function from a class which emits a signal, and that signal is connected to the slot which I need


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.