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. Getting object from a different QML file
Forum Updated to NodeBB v4.3 + New Features

Getting object from a different QML file

Scheduled Pinned Locked Moved QML and Qt Quick
12 Posts 6 Posters 19.0k Views 1 Watching
  • 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.
  • J Offline
    J Offline
    jech
    wrote on last edited by
    #1

    Hello, I can't find a way to locate an element (by it's ID) in a component (a separate .qml file). Let's say I have a main QML file. It contains a ListView component which is in a separate file. I need to call the positionViewAtIndex() function of the ListView. How to do it?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      krishkn
      wrote on last edited by
      #2

      If i understood ur question correct, u already hv listView Component id defined inside the main QML file. U can call the positionViewAtIndex() defined inside ListView component just like any other function.Say use a scriptAction on someVariable {script:listViewId.positionViewAtIndex() }

      Regards,
      Krishkn

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jech
        wrote on last edited by
        #3

        Unfortunately it's more complicated. I have not expressed it clear enough.

        The ListView is a sub component of an item in main QML. So I don't see the ID of the ListView from the main window.

        But your suggestion gave me an idea I'm gonna try. I'll write a function of the item visible to main QML which will call the function of ListView. I believe this should work, but I'm not sure if this is the preferred way of doing it.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          CreMindES
          wrote on last edited by
          #4

          I'm not sure I understans your problem fully, but if your solution doesn't work, here another thought-provoking:

          If you have a component in a separete qml, you can just write the following:

          @
          Qmlfilename { //the first letter should be capital
          id: youchoose
          }
          @

          and then, if you want to make a statechange your simple write:

          @
          PropertyChanges {
          target: idyouchoosed
          property: new value
          @

          I'm not sure it will help, but maybe ;)

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jech
            wrote on last edited by
            #5

            I need to call a function, not share a property. But the idea is basically the same. I just tried it and it works. But there are 4 functions which call each other before calling the final function. I'm not sure if this is the most effective way.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              CreMindES
              wrote on last edited by
              #6

              I see now, sorry for hte misunderstood. yeah, it sounds a little overcomplicated, I think that there should be a simpler way... Maybe with a slots...

              1 Reply Last reply
              0
              • S Offline
                S Offline
                syrianzoro
                wrote on last edited by
                #7

                may be I have an idea , you can use property to assign value to the item you want with out call it by id let its file do that

                Qt is the future

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mohsen
                  wrote on last edited by
                  #8

                  Hello,
                  Let me say you can't directly have access to objects on other qml files. Actually, every separated file in .qml extension will be known as an object with self properties and functions but the functions inside could be called by other qml files. Also you may use "property alias" method to have direct access to your target object properties. (see http://doc.qt.nokia.com/4.7-snapshot/qml-extending-types.html#property-aliases)

                  Here i will show you an example on how to call functions by other pages:
                  TestObject\TestObject.qml
                  @import Qt 4.7

                  Rectangle {
                  id: rect
                  width: 50
                  height: 50
                  color: "white"

                  function changecolor(pColor)
                  {
                      rect.color=pColor;
                  }
                  

                  }@
                  Demo.qml
                  @import Qt 4.7
                  import "TestObject"

                  Rectangle {
                  width: 300
                  height: 300

                  TestObject{
                      id: tobj
                      x:0
                      y:0
                  }
                  
                  MouseArea{
                      anchors.fill: parent
                      onClicked: tobj.changecolor("black");
                  }
                  

                  }@

                  See also http://wiki.forum.nokia.com/index.php/CS001625_-_Connecting_Qt_signal_to_QML_function to learn how to join C++ code with QML

                  Good Luck!

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jech
                    wrote on last edited by
                    #9

                    mohsen> Thank you, this is exactly how I solved it. But because I use a more complicated structure of objects I ended up with calling four functions in a row (from PySide call the main QML function, call it's QML object's function (repeat twice) and then call the final function). It seemed to me to complicated, so I thought there should be an easier way.

                    But if I think more deeply about it, there can't be anything simpler. The program doesn't know that I used the object only once. I might have more instances of the same object and which of them would be called then....

                    1 Reply Last reply
                    0
                    • X Offline
                      X Offline
                      xsacha
                      wrote on last edited by
                      #10

                      You don't need a function in that example. A simple alias should suffice.

                      • Sacha
                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        jech
                        wrote on last edited by
                        #11

                        xsacha> I have tried that but it seems to me, that I can't have an alias for a function, only for a property. Please note, that I need to call a function at the end, not set a property.

                        1 Reply Last reply
                        0
                        • X Offline
                          X Offline
                          xsacha
                          wrote on last edited by
                          #12

                          Oh, I thought it was just setting color like in the example. Nevermind ;)

                          • Sacha
                          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