Read of delegate objectName
-
We need more info to understand what do you want to do. But since
delegate
property isComponent
you cannot acces anything by idrectnagleId
. This id is internal only for that rectangle. -
As I said, in fact your delegate isn't exist like an any item. ListView will instantiate such items by demand, you can access only really existed items.
-
As I said, in fact your delegate isn't exist like an any item. ListView will instantiate such items by demand, you can access only really existed items.
@intruderexcluder
How can I then test the property of this delegate with Unittest? -
Not sure, but if you want to test only
objectName
property you can create dummy component and then create object from this component. Getdelegate
property of your ListView and then create object viaQQmlComponent
orQt.createComponent
depends from which side you want to test your delegate. Hope this can help. -
In case of QML you can create Rectangle object directly from delegate property:
ListView { id: view objectName: "listview" delegate: Rectangle { objectName: "delegaterect" } } Component.onCompleted: { let d = view.delegate.createObject(null); console.log(d.objectName); d.Component.destruction.connect(() => { console.log("READY TO BE DESTRUCTED"); }); d.destroy(); }
In case of C++ side code can be something like:
auto lv = engine.rootObjects()[0]->findChild<QObject*>("listview"); auto component = lv->property("delegate").value<QQmlComponent*>(); auto delegate = component->create(); qDebug() << delegate->objectName(); delegate->deleteLater();
-
Hello everybody,
I have something like this:
QML:ListView{ id: listViewId objectName:"listViewObj" .... delegate: Rectangle { id: rectnagleId objectName: "rectangleDelegate" .... } }
and how can i get the property from the delegate?
@galilio said in Read of delegate objectName:
ListView{ id: listViewId objectName:"listViewObj" .... delegate: Rectangle { id: rectnagleId objectName: "rectangleDelegate" .... } } //to get item and access the properies function getItemAtIndex(index){ var item = listViewId.itemAtIndex(index); if(item){ console.log(item.objectName) } }
-
@galilio said in Read of delegate objectName:
ListView{ id: listViewId objectName:"listViewObj" .... delegate: Rectangle { id: rectnagleId objectName: "rectangleDelegate" .... } } //to get item and access the properies function getItemAtIndex(index){ var item = listViewId.itemAtIndex(index); if(item){ console.log(item.objectName) } }
-
@galilio fair enough.
alright, trick 17 (untested)
ListView{ id: listViewId objectName:"listViewObj" var testObject: undefined signal assignToObject(var id) .... delegate: Rectangle { id: rectnagleId objectName: "rectangleDelegate" Connections{ target: listViewId onAssignToObject: if(index === id) listViewId.testObject = rectnagleId } .... } } //to get item and access the properies onTestObjectChanged:{ if(testObject) console.log(testObject.objectName) }
-
@j-hilk said in Read of delegate objectName:
onAssignToObject: if(index === id) listViewId.testObject = rectnagleId
what is your id here
Thanks -
@j-hilk said in Read of delegate objectName:
onAssignToObject: if(index === id) listViewId.testObject = rectnagleId
what is your id here
Thanks@galilio said in Read of delegate objectName:
@j-hilk said in Read of delegate objectName:
onAssignToObject: if(index === id) listViewId.testObject = rectnagleId
what is your id here
Thanksthe argument of the signal
signal assignToObject(var id)
-
@galilio
works fine in my test fileimport QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.0 Window { visible: true width: 400 height: 750 title: qsTr("Hello World") id: root property int testIndex: 0 Timer{ running: true interval: 1000 repeat: true onTriggered: { listViewId.assignToObject(root.testIndex) testIndex++ if(testIndex >= 10) testIndex = 0 } } ListView{ id: listViewId anchors.fill: parent objectName:"listViewObj" property var testObject: undefined onTestObjectChanged:{ if(testObject) console.log(testObject.objectName) } signal assignToObject(var id) model: 10 spacing: 2 delegate: Rectangle { id: rectnagleId objectName: "rectangleDelegate" + index Text { text: modelData anchors.centerIn: parent } width: listViewId.width height: 20 color: "green" Connections{ target: listViewId onAssignToObject: if(index === id) listViewId.testObject = rectnagleId } } } }
-
I want to write a unittest for the QML file.
I could access all objects using ObjectName and check the property.at delegate I can not use objectname and my question how can I access the property within this delegate?
-
Delegate you can't access using the object name. These are dynamically created object.