Getting object from a different QML file



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



  • 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() }



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



  • 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 ;)



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



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



  • 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



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



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



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



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



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


Log in to reply
 

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