Hi all,
I am new to QML and I am currently working at a reusable QML component.
It is a ListView with a model class attached.
The model is derived from QStandardItemModel with custom roles defined.
Furthermore, the ListView uses a delegate component to render the data attached to the role.
As I have got several model sub classes I would like to reuse my ListView with different models which in the end all define different roles.
So far, everything works fine except re-use.
In order to reuse my ListView I think I have to assign roles more dynamically e.g. configure the role the delegate should use for rendering from outside of my ListView component.
Here is a short example of what I would like to achieve:
main.qml:
Rectangle
{
MyListView
{
id: taskList
model: taskModel //QStandardItemModel
role: CustomDisplayText//<-role assignment does not work this way
}
}
Rectangle
{
MyListView
{
id: taskList
model: taskModel //QStandardItemModel
role: CustomDisplayText//<-role assignment does not work this way
}
}
To copy to clipboard, switch view to plain text mode
MyListView.qml:
MyListView
{
id: myListView
objectName: "myListView"
property variant role
delegate: Rectangle
{
id: itemDelegate
objectName: "itemDelegate"
color: itemDelegate.focus? highlightColor : backgroundColor
Text
{
id: itemTextField
objectName: "itemTextField"
anchors.verticalCenter: itemDelegate.verticalCenter
text: role
}
MouseArea
{
id: itemDelegateMouseArea
objectName: "itemDelegateMouseArea"
anchors.fill: parent
onClicked:
{
itemDelegate.forceActiveFocus();
itemSelected(model.index)
}
}
}
}
MyListView
{
id: myListView
objectName: "myListView"
property variant role
delegate: Rectangle
{
id: itemDelegate
objectName: "itemDelegate"
color: itemDelegate.focus? highlightColor : backgroundColor
Text
{
id: itemTextField
objectName: "itemTextField"
anchors.verticalCenter: itemDelegate.verticalCenter
text: role
}
MouseArea
{
id: itemDelegateMouseArea
objectName: "itemDelegateMouseArea"
anchors.fill: parent
onClicked:
{
itemDelegate.forceActiveFocus();
itemSelected(model.index)
}
}
}
}
To copy to clipboard, switch view to plain text mode
In fact, the role assignment does not work this way.
Can anyone help me solving my problem?
Thanks in advance!
Bookmarks