[Development] Behavior change in QtQuick 2.0 Qml ListModel (for javascript date object)

joona.t.petrell at nokia.com joona.t.petrell at nokia.com
Mon Jun 25 23:45:43 CEST 2012


Hi Nils,

> Bug or feature?

Sounds like a bug, though your code will work in Qt Quick 2.0 if you enable dynamic roles in your ListModel, that is write ListModel { dynamicRoles: true }. Due to performance reasons ListModel in Qt Quick 2.0 has changed to use static roles by default instead of dynamic roles (see http://doc-snapshot.qt-project.org/5.0/qml-qtquick2-listmodel.html#dynamicRoles-prop). The new code path seems to break the Date object. It may also be that there is a reason why ListModel does not support the Date object, but if so it is not immediatelly obvious from the documentation what native JavaScript types can be stored in ListModel and what not.

Cheers,

Joona
________________________________________
From: development-bounces+joona.t.petrell=nokia.com at qt-project.org [development-bounces+joona.t.petrell=nokia.com at qt-project.org] on behalf of ext Nils Jeisecke [njeisecke at saltation.de]
Sent: Monday, June 25, 2012 4:15 PM
To: development at qt-project.org
Subject: [Development] Behavior change in QtQuick 2.0 Qml ListModel (for        javascript date object)

Hi,

the following code works just fine with Qt Quick 1.1.
However porting this to 2.0 gives the following output:

date is [object Object]
file:///...q2listmodel.qml:9: TypeError: Object #<Object> has no
method 'getMonth'

Does the new ListModel/ListElement implementation apply some automatic
conversion for javascript date objects?
Putting other javascript objects in there works just fine.

---
import QtQuick 1.1  /* 2.0 */

Item {
  ListModel { id: listModel }

  Component.onCompleted: {
    listModel.append({ "date": new Date } )
    console.log("date is " + listModel.get(0).date)
    console.log("month is " + listModel.get(0).date.getMonth())
  }
}
---

Interestingly wrapping the date object in another object works:
---
import QtQuick 2.0

Item {
  ListModel { id: listModel }

  Component.onCompleted: {
    listModel.append({ date: { x: new Date } }) /* wrap into x */
    console.log("date is " + listModel.get(0).date.x)
    console.log("month is " + listModel.get(0).date.x.getMonth())
  }
}
---

Bug or feature?

Nils
_______________________________________________
Development mailing list
Development at qt-project.org
http://lists.qt-project.org/mailman/listinfo/development



More information about the Development mailing list