[Interest] Why does QML prefer 'count' over '.length'?

Mitch Curtis mitch.curtis at qt.io
Thu Jul 28 09:52:26 CEST 2016

> -----Original Message-----
> From: Interest [mailto:interest-bounces+mitch.curtis=qt.io at qt-project.org]
> On Behalf Of Jason H
> Sent: Wednesday, 27 July 2016 4:32 PM
> To: J-P Nurmi <jpnurmi at qt.io>
> Cc: interest at qt-project.org
> Subject: Re: [Interest] Why does QML prefer 'count' over '.length'?
> > Sent: Tuesday, July 26, 2016 at 4:41 PM
> > From: "J-P Nurmi" <jpnurmi at qt.io>
> > To: "interest at qt-project.org" <interest at qt-project.org>
> > Subject: Re: [Interest] Why does QML prefer 'count' over '.length'?
> >
> > On Tuesday, July 26, 2016 22:04,Jason H <jhihn at gmx.com> wrote:
> > > JS uses length on arrays and strings. Anytime you have an array (be
> > > it a list, etc) the number of items is denoted as .length
> >
> > And in QML you have .length where you'd expect to have it, in JS arrays
> and strings.
> >
> > > It makes no sense to have Qt use count in similar situations.
> >
> > Arrays and strings are not exactly similar situations to item views
> > and item models. :)
> >
> > > As Thiago mentioned length, size and count are all analogous in the
> C++ API, so I don't know how/why they are ambiguous.
> >
> > The C++ API of the Qt containers. Not item views nor item models. Two
> entirely different worlds. The length or size of an array is clear, but
> the length or size of a list view is likely to get associated to the UI
> element's visual geometry.
> I just don't see it. Qt's usage of Height and width are perfectly
> consistent. Why have a special rule about Qt containers using count()
> whent he JS containers use length.

It makes perfect sense. "count" makes sense for visual things. "length" only means "amount" in computer science, and only then in the realm of containers. You don't say "white blood cell length" when referring to the amount of blood white cells, you say "white blood cell count". You don't say "the length of items in a view", you say "the count/amount of items in a view".

Names should be intuitive, and often the best way to do that is to be analogous to real life.

The correct response to

"What is the combined length of your pets?"

is fear and confusion.

Whereas a valid response to

"How many pets do you have?"


"Oh, three."

> That's not a potential confusion, that's a real confusion today:

This is the first time I've heard anyone have a problem with it.

> property variant items: [1, 2, 3, "four", "five"] // this will use length
> ListModel{ id: items; ... } // this will use count.

One is a JavaScript type for storing almost any type of data, the other is a Qt type whose sole purpose is for use with visual types.

> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest

More information about the Interest mailing list