Hello!
I want to create a QML application where I have a bunch of Buttons that you can click on. These Buttons are categorized into groups.
At the top of the page there are CategoryButtons. When you click on one of the CategoryButtons all the Buttons in that category appear on the page. The Buttons not in that category remain hidden.
I have two models, one for the CategoryButtons and one for the Buttons.
Here are the models, just filled with random data:
ListModel {
id: categoryButtonsModel
ListElement {
name: "Super Awesome Category"
filterKey: "super"
}
ListElement {
name: "Slightly More Awesome Category"
filterKey: "slight"
}
}
ListModel {
id: buttonsModel
ListElement {
name: "Cool Button"
filterKey: "super"
}
ListElement {
name: "Geek Button"
filterKey: "super"
}
ListElement {
name: "Hipster Button"
filterKey: "slight"
}
}
ListModel {
id: categoryButtonsModel
ListElement {
name: "Super Awesome Category"
filterKey: "super"
}
ListElement {
name: "Slightly More Awesome Category"
filterKey: "slight"
}
}
ListModel {
id: buttonsModel
ListElement {
name: "Cool Button"
filterKey: "super"
}
ListElement {
name: "Geek Button"
filterKey: "super"
}
ListElement {
name: "Hipster Button"
filterKey: "slight"
}
}
To copy to clipboard, switch view to plain text mode
I have added a property to all of these called filterKey. I want all Buttons to be in the category that has the same filterKey that they have. I use a DelegateModel to filter for filterKey:
DelegateModel {
id: buttonsDelegateModel
model: buttonsModel
delegate: Button {
text: name
}
filterOnGroup: "super"
groups: [
DelegateModelGroup {
includeByDefault: false
name: "super"
Component.onCompleted: {
for (var i = 0; i < buttonsModel.count; i++ ) {
var entry = buttonsModel.get(i);
if(entry.filterKey === name) insert(entry);
}
}
},
DelegateModelGroup {
includeByDefault: false
name: "slight"
Component.onCompleted: {
for (var i = 0; i < buttonsModel.count; i++ ) {
var entry = buttonsModel.get(i);
if(entry.filterKey === name) insert(entry);
}
}
}
]
}
DelegateModel {
id: buttonsDelegateModel
model: buttonsModel
delegate: Button {
text: name
}
filterOnGroup: "super"
groups: [
DelegateModelGroup {
includeByDefault: false
name: "super"
Component.onCompleted: {
for (var i = 0; i < buttonsModel.count; i++ ) {
var entry = buttonsModel.get(i);
if(entry.filterKey === name) insert(entry);
}
}
},
DelegateModelGroup {
includeByDefault: false
name: "slight"
Component.onCompleted: {
for (var i = 0; i < buttonsModel.count; i++ ) {
var entry = buttonsModel.get(i);
if(entry.filterKey === name) insert(entry);
}
}
}
]
}
To copy to clipboard, switch view to plain text mode
This code above works. I have one problem only:
The groups property of the above DelegateModel is basically some generic code and the filterKeys in CategoryButtonsModel. I want to generate groups based on CategoryButtonsModel so I don't have to specify the same things two times.
How can I generate the groups list from CategoryButtonsModel dynamically?
Thank you for your time.
Regards,
Viki
Bookmarks