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 ;)
-
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.7Rectangle {
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: 300TestObject{ 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....