Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Pass data between QML files

Pass data between QML files

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 3 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mandruk1331M Offline
    mandruk1331M Offline
    mandruk1331
    wrote on last edited by A Former User
    #1

    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

    Naveen_DN E 2 Replies Last reply
    0
    • mandruk1331M mandruk1331

      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.

      Naveen_DN Offline
      Naveen_DN Offline
      Naveen_D
      wrote on last edited by Naveen_D
      #2

      @mandruk1331 hi, can you post the code

      Naveen_D

      1 Reply Last reply
      0
      • mandruk1331M mandruk1331

        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.

        E Offline
        E Offline
        Eeli K
        wrote on last edited by
        #3

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

        mandruk1331M 1 Reply Last reply
        0
        • E Eeli K

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

          mandruk1331M Offline
          mandruk1331M Offline
          mandruk1331
          wrote on last edited by
          #4

          @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

          E 1 Reply Last reply
          0
          • mandruk1331M mandruk1331

            @Eeli-K there's not much code:

                   QQmlApplicationEngine *engine = new QQmlApplicationEngine ;
                  engine->rootContext()->setContextProperty("Object",&O);
                  engine->load(QUrl(QLatin1String("qrc:/some_qml.qml")));
            
            E Offline
            E Offline
            Eeli K
            wrote on last edited by
            #5

            @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?

            mandruk1331M 1 Reply Last reply
            0
            • E Eeli K

              @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?

              mandruk1331M Offline
              mandruk1331M Offline
              mandruk1331
              wrote on last edited by
              #6

              @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

              Mandruk1331

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved