I am trying to change the delegate of qml listview from C++ but currently I stuck at how to change the alias which represents the delegate property.
Update on details: I have multiple delegates in separated qml files, in my application there are many screens, each screen will have different UI of listview, what I want is something like:
Pass delegate file name to C++ function >>> C++ function set delegate property of listView (or thing like that) >>> listview loads corresponding delegate.
My qml file looks like:
Item {
id: root
property alias listViewDelegate: listView.delegate
ListView{
id: listView
delegate: MyDelegate{} // I have MyDelegate.qml file, it's working well
model: listModel
}
// List model
MyListModel {
id: listModel
}
}
I tried to change listViewDelegate alias from C++ using setProperty() method but got no luck (error in fact).
qmlObj->setProperty("listViewDelegate", componentDelegate);
How to achieve this? Or anyone can suggest me the better method to achieve it? Thanks!
Thank everyone, I just figured out a way to do that by using javascript, seems complicated but it works.
I add this javascript function into my root Item
function loadListViewDelegate(file){
var component = Qt.createComponent(file);
if(component && (component.status === Component.Ready))
{
listView.delegate = component;
return file;
}
return "";
}
Then I invoke this function from C++, with parameter is the delegate qml file. It looks like this:
QMetaObject::invokeMethod(qmlObj, "loadListViewDelegate", Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, "delegate_screen_home.qml"));
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments