Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
treike
Posts: 18
Registered: ‎09-26-2012
My Device: Developer
Accepted Solution

GroupDataModel formatting header text

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?

Regular Contributor
ametller
Posts: 54
Registered: ‎07-15-2010
My Device: Not Specified

Re: GroupDataModel formatting header text

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...

Developer
treike
Posts: 18
Registered: ‎09-26-2012
My Device: Developer

Re: GroupDataModel formatting header text

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 :-(

 

 

Regular Contributor
ametller
Posts: 54
Registered: ‎07-15-2010
My Device: Not Specified

Re: GroupDataModel formatting header text

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?

Developer
treike
Posts: 18
Registered: ‎09-26-2012
My Device: Developer

Re: GroupDataModel formatting header text

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.

Regular Contributor
ametller
Posts: 54
Registered: ‎07-15-2010
My Device: Not Specified

Re: GroupDataModel formatting header text

And what if you do:

 

ListItemComponent {
	type: "header"
        Label {
        	text: ListItemData.dataString
        }
},

 being "dataString" the field with the formatted string in your custom GroupDataModel?

Developer
treike
Posts: 18
Registered: ‎09-26-2012
My Device: Developer

Re: GroupDataModel formatting header text

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 :-)

Regular Contributor
ametller
Posts: 54
Registered: ‎07-15-2010
My Device: Not Specified

Re: GroupDataModel formatting header text

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):

 

http://supportforums.blackberry.com/t5/Cascades-Development-Knowledge/Using-your-own-DataModel/ta-p/...

 

I hope it helps!

Developer
treike
Posts: 18
Registered: ‎09-26-2012
My Device: Developer

Re: GroupDataModel formatting header text

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.

Regular Contributor
ametller
Posts: 54
Registered: ‎07-15-2010
My Device: Not Specified

Re: GroupDataModel formatting header text

[ Edited ]

This is a nice approach in my opinion! I can't think in a simpler solution. Good job!