Unsolved Read of delegate objectName
-
I really did not understand that exactly.
can you please explain to me more exactly with an example?many thanks
-
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();
-
@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) } }
-
@j-hilk
my qt version is 5.9.1 -
@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 -
@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)
-
This post is deleted! -
Sorry:
nothing happens
Slot was not called -
@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 } } } }
-
and how is it if I want to test the delegate with unit test (in cpp) sorry
-
@Galilio how do you access the objects using objectName can you share some code
-
Delegate you can't access using the object name. These are dynamically created object.