10-01-2012 10:44 AM
I would like to know if it is possible to format the header in a GroupDataModel in some way?
I have a GroupDataModel that groups by Date (a QDateTime). But I don't want an output like: 2012-10-01T10:02:48, I want to have it like" 2012/10/01".
Is that possible? Or what else could I do?
Solved! Go to Solution.
10-01-2012 10:58 AM
Hello mate.
What I did was to convert the QDate and the QTime objects to a String using the format I wanted, before adding it to the GroupDateModel.
QDate posDate; // posDate initialization comes here... QString finalFormattedDate = posDate.toString(Qt::SystemLocaleLongDate);
or
QTime posTime;
// posTime initialization comes here...
QString finalFormattedTime = posTime.toString("hh:mm");However, I didn't have the need to order it by date and/or time. By using a String could make it quite tricky...
10-01-2012 11:03 AM
Yes, that's what I've done before, I've formatted it to a string with german locale (dd.MM.yy).
But then sorting ASC/DESC isn't really right. When sorting DESC 23.09.2012 is sorted in before 01.10.2012.
Dates descending should be like 01.10.2012 and then 23.09.2012.
That's the problem :-(
10-01-2012 11:11 AM
Oh I see...
Now you have the "data" field in your GroupDataModel that is the QDateTime element. You use it to sort the list, correct? Maybe what you could do is to create another field (named "dataString" for example) that will contain the string after formatting the data, and that will be used for output. To sum up, the QDateTime field will be used to sort the list, while the string field will contain the string that will be outputted. Does it make sense?
10-01-2012 11:45 AM
Makes perfectly sense, but doesn't seem to work with a GroupDataModel.
The header items output is just
ListItemComponent {
type: "header"
Label {
text: ListItemData
}
},
Other than the type: "item"-items I think any additional attributes of the objects in the ListView are not accessible in the Header.
10-01-2012 12:49 PM
And what if you do:
ListItemComponent {
type: "header"
Label {
text: ListItemData.dataString
}
},being "dataString" the field with the formatted string in your custom GroupDataModel?
10-01-2012 01:08 PM
No, I just get nothing as headers then.
Maybe I don't get it right. I am using the default GroupDataModel, just filling it with my special objects.
The documents state:
"Also note that ListItemData is a QString (there's no properties on it) for headers, but a QVariantMap (with QString properties "firstName" and "lastName") for other items."
So, in the default GroupDataModel there is no possiblity to do this, I think.
I didn't yet try to derive a custom GroupDataModel, still have to read a bit more about it to get into it :-)
10-01-2012 01:29 PM
Yes, you are totally right. I forgot that it's not possible to use any ListItemData properties when working with headers, I don't know the reason.
I also think you should create a custom GroupDataModel. I'm not into custom GroupDataModels neither, but take a look to this example (well it uses a DataModel, not GroupDataModel):
I hope it helps!
10-01-2012 03:28 PM
This is how I've done it now:
Declared my own CustomGroupDataModel derived from GroupDataModel and just override the GroupDataModel::data() method like this:
QVariant CustomGroupModel::data(const QVariantList& indexPath) {
// if it is a header
if (this->hasChildren(indexPath)){
QDate headerResult = GroupDataModel::data(indexPath).toDate();
QString headerFormatted = headerResult.toString("dd.MM.yy");
return QVariant(headerFormatted);
}
else{
QVariant mData = GroupDataModel::data(indexPath);
return mData;
}
}
So, if the Item at "indexPath" has children it's a header, b/c GroupDataModels only have to have headers and items, no grandchildren.
Maybe it's possible to do it even easier/faster, but that's how I did it now. I'd love to know better ways.
10-01-2012 05:17 PM - edited 10-01-2012 05:18 PM
This is a nice approach in my opinion! I can't think in a simpler solution. Good job!