Discussion:
[Development] QML and QAbstractListModel
Alberto Mardegan
2013-01-10 16:46:14 UTC
Permalink
Hi all!
I'd like to make C++ models more usable from QML; in the net there
are several blog posts illustrating how to achieve that, but IMHO it
would be better if at least some of these handy features were in
QAbstractListModel itself:

- "count" property
- "get(index)" invocable method, returning a QVariantMap mapping all the
roles to their data for the specified index

Also, though not very often requested, we could add:
- "remove(index)" which just calls QAbstractItemModel::removeRow()
- "QList<QVariant> items(const QString &role)", which returns the data
for the role "role" for all items.

The implementation for the above is fairly trivial, so I wonder if
there's some other reasons why it hasn't been done or if it's just that
no one did and a patch is welcome.

Ciao,
Alberto
Alan Alpert
2013-01-10 17:29:42 UTC
Permalink
On Thu, Jan 10, 2013 at 8:46 AM, Alberto Mardegan
Post by Alberto Mardegan
Hi all!
I'd like to make C++ models more usable from QML; in the net there
are several blog posts illustrating how to achieve that, but IMHO it
would be better if at least some of these handy features were in
- "count" property
- "get(index)" invocable method, returning a QVariantMap mapping all the
roles to their data for the specified index
+1 from me, those would clearly make it more QML-friendly.
Post by Alberto Mardegan
- "remove(index)" which just calls QAbstractItemModel::removeRow()
- "QList<QVariant> items(const QString &role)", which returns the data
for the role "role" for all items.
I'm on the fence about adding those. Since they wouldn't be commonly
used, maybe it would be better to wait until there's time for a more
holistic 'models in QML' update (which likely means waiting a long
while, but eventually QML and QtQuick will need to deal with the issue
of full tree model support).
Post by Alberto Mardegan
The implementation for the above is fairly trivial, so I wonder if
there's some other reasons why it hasn't been done or if it's just that
no one did and a patch is welcome.
To my knowledge it's the latter. There's a lot of Qt API which could
be made more QML friendly, especially in this way (just adding
properties and convenience functions, for when a user subclass is
exposed to QML).

--
Alan Alpert
Charley Bay
2013-01-10 19:06:12 UTC
Permalink
Post by Alan Alpert
Post by Alberto Mardegan
Hi all!
I'd like to make C++ models more usable from QML; in the net there
are several blog posts illustrating how to achieve that, but IMHO it
would be better if at least some of these handy features were in
- "count" property
- "get(index)" invocable method, returning a QVariantMap mapping all the
roles to their data for the specified index
+1 from me, those would clearly make it more QML-friendly.
Post by Alberto Mardegan
- "remove(index)" which just calls QAbstractItemModel::removeRow()
- "QList<QVariant> items(const QString &role)", which returns the data
for the role "role" for all items.
I'm on the fence about adding those. Since they wouldn't be commonly
used, maybe it would be better to wait until there's time for a more
holistic 'models in QML' update (which likely means waiting a long
while, but eventually QML and QtQuick will need to deal with the issue
of full tree model support).
Post by Alberto Mardegan
The implementation for the above is fairly trivial, so I wonder if
there's some other reasons why it hasn't been done or if it's just that
no one did and a patch is welcome.
To my knowledge it's the latter. There's a lot of Qt API which could
be made more QML friendly, especially in this way (just adding
properties and convenience functions, for when a user subclass is
exposed to QML).
Funny this should be posted -- I'm coding a "wrapper" for a bunch of
app-specific QML/QAIM right now.

I assume we are all referencing Chris' blog post from two years ago:

http://cdumez.blogspot.com/2010/11/how-to-use-c-list-model-in-qml.html

...and Benoît's referencing blog from a year ago with some "extensions":

http://bsauts-en.blogspot.com/2011/09/listmodel-in-c-exposed-to-qml.html

...and stuff like QML "read/write (role)" updates:

http://qt-project.org/forums/viewthread/21787/

...and making a "model" a property of an item within another "model":

http://qt-project.org/forums/viewthread/6906

There's not tons of C++QAIM/QML blog posts out there, so please jump in if
you know of others. Generally, I'm trying to understand
"accepted-convention" regarding these, and implement my "template-wrappers"
which look like:

QAbstractItemModel
=> (derive) MyItemModel (abstract with signals/slots/properties)
=> (derived) MyItemModelBase<> (template with app-specific hooks
possible)
=> (derived) MyAppSpecificModel (many of these)

This is a pattern I've used quite a lot with Qt (others have also), and it
works:

- MyItemModel implements stuff I always want "extended", has
signals/slots/properties, and runs through "moc"

- MyItemModelBase<> does NOT run through "moc" (it's a template, you
can't), but has common implementation that may be "overridden" in
application-specific-type-derived-cases. (Derived classes merely override
the MyItemModelBase<>::SomeFunc())

- MyAppSpecificModel - may run through "moc", but usually I don't need
additional "signals/slots/properties" (anything needed was commonly
provided in "MyItemModel").

What's interesting about the QAIM/QML thing (to me) is abstracting the
concept of the "Item-within-the-Model" with "properties/roles" exposed to
QML. Chris' example illustrated this with his "ListItem" (derived from
"QObject"). But, I think it would more generically be handled with that
common pattern I tend to use:

QObject
=> MyModelItem (with "signals/slots/properties")
=> MyModelItemBase<> (template, no "moc")
=> MyAppSpecificItem (many of these)

QUESTION: Alan seems to suggest the (C++) QAIM/QML might be
changed/updated in the "near-future" (and that was the purpose for the
question in this thread). Is that to handle the "model" or the "item", or
"both"?

QUESTION #2: I *assumed* (perhaps wrongly) that QAbstractItemModel DID NOT
have "Q_PROPERTY()" things because the "Q_PROPERTY()" is purported to
expose attributes to QML. I see "Q_PROPERTY()" as an "adapter-to-QML".
So, it seemed obvious (to me) that all "Q_PROPERTY()" things would go into
"MyItemModel", and not the "QAbstractItemModel" itself. Any
"common-properties" (like "count()" and "get(int)") could similarly be in a
derived "QAbstractItemModelWrappedForQml". Is the intent to make it
"open-season" on adding "Q_PROPERTY" things to other C++ classes, for those
attributes to be exposed to QML? I'm not sure that's a good idea, because
it seems like the "act-of-wrapping/exporting" to QML is a very different
level API than the act of C++ interfacing with the primitive C++ class.
(This isn't a strongly held view, it's just my current impression.)

An additional design need I have is for "generic-sets-of-things" to be
"referenced" by "MyAppSpecificModel" instances, so that adding/changing the
"set" is "reflected" in the QML. I'm pretty sure this can be implemented
in a generic fashion with the pattern listed above. I wouldn't do this
work, or I would "retire" this work, if Qt-proper were going to take that
on. My specific work is for these design goals:

- App-specific "model" that "reflects" the "set-of-app-specific-items"
(with all relevant "updates" into QML when an item-in-the-set-changes)

- App-specific "model" is implicitly composed of app-specific "items" with
their own unique "roles/properties" exposed to QML (very important so QML
delegates can be written)

- App-specific "model" is defined with only a few lines of code (e.g.,
derive from template-base)

Is anyone else doing work like this? For example, I could consider using
classes like:

- QAbstractItemList
=> QAbstractItemListWithQmlProperties (FICTITIOUS)

- QItemForModelExposedToQml (FICTITIOUS)

...although I use templates quite a lot as the "base" class because the
"re-use" is higher, and the implementation does not go through virtual
functions. (This pattern is done by me and others, but is not commonly
seen within the Qt code base itself.)

--charley
Alan Alpert
2013-01-10 19:39:32 UTC
Permalink
QUESTION: Alan seems to suggest the (C++) QAIM/QML might be changed/updated
in the "near-future" (and that was the purpose for the question in this
thread). Is that to handle the "model" or the "item", or "both"?
Whoa, when I said "maybe it would be better to wait until there's time
for a more
holistic 'models in QML' update" I was not talking "near-future". I
was talking far-future. So if that's what you're referring to then
keep in mind I was walking about an open-ended long-term research
project. Not something that I think anyone has time to work on this
year.

In the near-future, all I can say is that QAIM is not off-limits for
contributions ;) .
QUESTION #2: I *assumed* (perhaps wrongly) that QAbstractItemModel DID NOT
have "Q_PROPERTY()" things because the "Q_PROPERTY()" is purported to expose
attributes to QML. I see "Q_PROPERTY()" as an "adapter-to-QML". So, it
seemed obvious (to me) that all "Q_PROPERTY()" things would go into
"MyItemModel", and not the "QAbstractItemModel" itself. Any
"common-properties" (like "count()" and "get(int)") could similarly be in a
derived "QAbstractItemModelWrappedForQml". Is the intent to make it
"open-season" on adding "Q_PROPERTY" things to other C++ classes, for those
attributes to be exposed to QML? I'm not sure that's a good idea, because
it seems like the "act-of-wrapping/exporting" to QML is a very different
level API than the act of C++ interfacing with the primitive C++ class.
(This isn't a strongly held view, it's just my current impression.)
Q_PROPERTY is not QML specific. A good example (at least, one I ran
into recently), was QCoreApplication. It has properties, and they are
not because we want to expose QCoreApplication to QML directly. In
fact my current implementation of QQmlApplicationEngine writes a QML
wrapper object still instead of exposing QCoreApplication (for various
reasons).

Q_PROPERTY can be added to any QObject subclass same as it could in Qt
4.6. The only difference is to consider the possibility of QML
exposure to the extent it makes sense for that class. Which is why
some properties that improve QML exposure potential makes sense for
QALM, because its subclasses get exposed a lot by user code.

We certainly want to be careful about where we actually expose C++
classes to QML. Don't do that unless they have a good QML level API.
But that is a separate step to adding properties. I hope no Qt users
assume that every QObject derived class in Qt is prepared to work
perfectly should they register it into QML themselves.

--
Alan Alpert
Mark
2013-01-10 21:14:56 UTC
Permalink
Post by Charley Bay
Post by Alan Alpert
Post by Alberto Mardegan
Hi all!
I'd like to make C++ models more usable from QML; in the net there
are several blog posts illustrating how to achieve that, but IMHO it
would be better if at least some of these handy features were in
- "count" property
- "get(index)" invocable method, returning a QVariantMap mapping all the
roles to their data for the specified index
+1 from me, those would clearly make it more QML-friendly.
Post by Alberto Mardegan
- "remove(index)" which just calls QAbstractItemModel::removeRow()
- "QList<QVariant> items(const QString &role)", which returns the data
for the role "role" for all items.
I'm on the fence about adding those. Since they wouldn't be commonly
used, maybe it would be better to wait until there's time for a more
holistic 'models in QML' update (which likely means waiting a long
while, but eventually QML and QtQuick will need to deal with the issue
of full tree model support).
Post by Alberto Mardegan
The implementation for the above is fairly trivial, so I wonder if
there's some other reasons why it hasn't been done or if it's just that
no one did and a patch is welcome.
To my knowledge it's the latter. There's a lot of Qt API which could
be made more QML friendly, especially in this way (just adding
properties and convenience functions, for when a user subclass is
exposed to QML).
Funny this should be posted -- I'm coding a "wrapper" for a bunch of
app-specific QML/QAIM right now.
http://cdumez.blogspot.com/2010/11/how-to-use-c-list-model-in-qml.html
http://bsauts-en.blogspot.com/2011/09/listmodel-in-c-exposed-to-qml.html
http://qt-project.org/forums/viewthread/21787/
http://qt-project.org/forums/viewthread/6906
There's not tons of C++QAIM/QML blog posts out there, so please jump in if
you know of others.
<snip>
Jumping in here.

I don't want to "advertise" my blog, but since you asked for others,
here is mine [1]. If i'm posting it's quite often about QML with some
C++ wrapper stuff. I should probably post more since i know a lot more
QML then you can see on my blog.

[1] http://kdeblog.mageprojects.com/
Bache-Wiig Jens
2013-01-11 09:59:23 UTC
Permalink
Post by Alberto Mardegan
Hi all!
I'd like to make C++ models more usable from QML; in the net there
are several blog posts illustrating how to achieve that, but IMHO it
would be better if at least some of these handy features were in
- "count" property
- "get(index)" invocable method, returning a QVariantMap mapping all the
roles to their data for the specified index
+1 from me. That would clean up a lot of the ugly special casing we have to deal with whenever models are provided to components.
Post by Alberto Mardegan
- "remove(index)" which just calls QAbstractItemModel::removeRow()
- "QList<QVariant> items(const QString &role)", which returns the data
for the role "role" for all items.
The implementation for the above is fairly trivial, so I wonder if
there's some other reasons why it hasn't been done or if it's just that
no one did and a patch is welcome.
I would suspect it the latter and we just assumed there was a philosophy behind it. Another possibly low hanging fruit is to add some convenience for sorting models. But the model implementation differences make straight forward use of QSortFilterProxyModel problematic.

Jens
Alberto Mardegan
2013-01-12 11:40:26 UTC
Permalink
Post by Alberto Mardegan
I'd like to make C++ models more usable from QML; in the net there
are several blog posts illustrating how to achieve that, but IMHO it
would be better if at least some of these handy features were in
- "count" property
- "get(index)" invocable method, returning a QVariantMap mapping all the
roles to their data for the specified index
https://codereview.qt-project.org/#change,44602

Ciao,
Alberto
Nils Jeisecke
2013-01-12 22:59:05 UTC
Permalink
Hi,

what about adding a new Quick item for "enhanced QAbstractItemModel access".

ListModelAdapter {
id: myModelAdapter
sourceModel: myModel
}

ListView {
model: myModelAdapter.sourceModel
delegate: Text {
MouseArea {
onClicked: console.log(myModelAdapter.data(model.index, "myRole")
}
}
}

Something like this:

#ifndef QQUICKLISTMODELADAPTER_H
#define QQUICKLISTMODELADAPTER_H

#include <QObject>
#include <QAbstractItemModel>

class QQuickListModelAdapter : public QObject
{
Q_OBJECT
Q_PROPERTY(QAbstractItemModel *sourceModel READ sourceModel WRITE
setSourceModel NOTIFY sourceModelChanged)
Q_PROPERTY(int count READ count NOTIFY countChanged)

public:
explicit QQuickListModelAdapter(QObject *parent = 0);

void setSourceModel(QAbstractItemModel *model);
QAbstractItemModel *sourceModel() const;

Q_INVOKABLE QVariant data(int row, const QString &role);
Q_INVOKABLE bool setData(row, const QString &role, const QVariant &value);
/* other useful methods */

signals:
void sourceModelChanged();
};

#endif // QLISTMODELADAPTER_H

Nils
Alberto Mardegan
2013-01-13 11:40:17 UTC
Permalink
Post by Nils Jeisecke
Hi,
what about adding a new Quick item for "enhanced QAbstractItemModel access".
ListModelAdapter {
id: myModelAdapter
sourceModel: myModel
}
ListView {
model: myModelAdapter.sourceModel
delegate: Text {
MouseArea {
onClicked: console.log(myModelAdapter.data(model.index, "myRole")
}
}
}
That would also work just for QAbstractListModel, unless we find a way
to present QModelIndex to QML.
If the proposal of adding the "count" property and the "get" method to
QAbstractListModel doesn't get accepted, IMHO the best thing to do is to
provide either code snippets or a *C++* helper class to help implement
those things, rather then workaround the limitations from the QML side.
We are talking about C++ models in any case, so I see more fit a
solution on the C++ side.

Ciao,
Alberto
Johan Thelin
2013-01-13 12:09:59 UTC
Permalink
Sorry for top-posting. I'm writing from my phone.

I've had a class like this for a project of mine. It exposed signals for
adding, removing and changing rows as well as the count, data and setData
methods.

This helped me peek and poke at a QAbstractLisaModel from JavaScript.

Cheers,

Johan
Post by Nils Jeisecke
Post by Nils Jeisecke
Hi,
what about adding a new Quick item for "enhanced QAbstractItemModel
access".
Post by Nils Jeisecke
ListModelAdapter {
id: myModelAdapter
sourceModel: myModel
}
ListView {
model: myModelAdapter.sourceModel
delegate: Text {
MouseArea {
onClicked: console.log(myModelAdapter.data(model.index, "myRole")
}
}
}
That would also work just for QAbstractListModel, unless we find a way
to present QModelIndex to QML.
If the proposal of adding the "count" property and the "get" method to
QAbstractListModel doesn't get accepted, IMHO the best thing to do is to
provide either code snippets or a *C++* helper class to help implement
those things, rather then workaround the limitations from the QML side.
We are talking about C++ models in any case, so I see more fit a
solution on the C++ side.
Ciao,
Alberto
_______________________________________________
Development mailing list
http://lists.qt-project.org/mailman/listinfo/development
Joseph Crowell
2013-01-13 12:15:16 UTC
Permalink
Post by Johan Thelin
Sorry for top-posting. I'm writing from my phone.
I've had a class like this for a project of mine. It exposed signals
for adding, removing and changing rows as well as the count, data and
setData methods.
This helped me peek and poke at a QAbstractLisaModel from JavaScript.
I bet she loved it.
Post by Johan Thelin
Cheers,
Johan
On Jan 13, 2013 12:40 PM, "Alberto Mardegan"
Post by Nils Jeisecke
Hi,
what about adding a new Quick item for "enhanced
QAbstractItemModel access".
Post by Nils Jeisecke
ListModelAdapter {
id: myModelAdapter
sourceModel: myModel
}
ListView {
model: myModelAdapter.sourceModel
delegate: Text {
MouseArea {
onClicked: console.log(myModelAdapter.data(model.index,
"myRole")
Post by Nils Jeisecke
}
}
}
That would also work just for QAbstractListModel, unless we find a way
to present QModelIndex to QML.
If the proposal of adding the "count" property and the "get" method to
QAbstractListModel doesn't get accepted, IMHO the best thing to do is to
provide either code snippets or a *C++* helper class to help implement
those things, rather then workaround the limitations from the QML side.
We are talking about C++ models in any case, so I see more fit a
solution on the C++ side.
Ciao,
Alberto
_______________________________________________
Development mailing list
http://lists.qt-project.org/mailman/listinfo/development
_______________________________________________
Development mailing list
http://lists.qt-project.org/mailman/listinfo/development
André Somers
2013-01-14 08:29:41 UTC
Permalink
Post by Alberto Mardegan
Hi all!
I'd like to make C++ models more usable from QML; in the net there
are several blog posts illustrating how to achieve that, but IMHO it
would be better if at least some of these handy features were in
- "count" property
- "get(index)" invocable method, returning a QVariantMap mapping all the
roles to their data for the specified index
- "remove(index)" which just calls QAbstractItemModel::removeRow()
- "QList<QVariant> items(const QString &role)", which returns the data
for the role "role" for all items.
The implementation for the above is fairly trivial, so I wonder if
there's some other reasons why it hasn't been done or if it's just that
no one did and a patch is welcome.
I am not fan of this change. I think the API of QAIM is already very
complex. Adding more methods that basically only sort-of mirror existing
methods but for a more confined use-cases only makes that situation
worse. For instance, you now get a count() method that claims that it
returns the number of items in the model. What does that mean in the
context of a tree model? It makes QAIM even harder to understand for
people trying to get into writing models for Qt's item views.

If you really need a these methods exported to QML, I'd like to see that
done in a way that does not further complicate the C++ API.

Perhaps an alternative approach would be to create a proxy model for
exporting to QML. It could include all the methods you need, but it
would not complicate QAIM itself. It seems to me that all the methods
you need can be implemented in terms of the already existing
functionality in QAIM, so it would work on all of them. Instead of
exposing the C++ QAIM directly, you could create an instance of the
proxy on it and expose that proxy model to QML instead, providing all
the QML specific methods you need without complicating the base QAIM.

It would IMHO be even better to get access to the QML model API from
C++, making it possible to build 'native' QML models directly in C++.
Eventually, that will probably happen, but I guess more water will need
to flow through <insert your local major river> before that can be done.

André
Andrew den Exter
2013-01-14 09:20:16 UTC
Permalink
Post by André Somers
Perhaps an alternative approach would be to create a proxy model for
exporting to QML. It could include all the methods you need, but it
would not complicate QAIM itself. It seems to me that all the methods
you need can be implemented in terms of the already existing
functionality in QAIM, so it would work on all of them. Instead of
exposing the C++ QAIM directly, you could create an instance of the
proxy on it and expose that proxy model to QML instead, providing all
the QML specific methods you need without complicating the base QAIM.
Some of this functionality already exists in VisualDataModel through its
items (
http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-visualdatamodel.html#items-prop)
property, which among other things provides generic accessors for model
data.

There's a tutorial explaining it's use at
http://qt-project.org/doc/qt-5.0/qtquick/qtquick2-qml-dynamicview-tutorial.html
although
all the code snippets seem to have gone missing so its usefulness is
somewhat reduced and you might be better off looking at the example source
in the qtdeclarative repo (examples/quick/tutorials/dynamicview/) instead.
Post by André Somers
It would IMHO be even better to get access to the QML model API from
C++, making it possible to build 'native' QML models directly in C++.
Eventually, that will probably happen, but I guess more water will need
to flow through <insert your local major river> before that can be done.
What's a native QML model? The model types provided by QtQuick are
QAbstractItemModel implementations and while the views can accept a few
other basic types as model sources the internal abstraction for those types
isn't at the model API level but something highly specific to
VisualDataModel and wouldn't be of much use to anyone else.


Andrew
Alberto Mardegan
2013-01-14 10:07:36 UTC
Permalink
Post by André Somers
I am not fan of this change. I think the API of QAIM is already very
complex. Adding more methods that basically only sort-of mirror existing
methods but for a more confined use-cases only makes that situation
worse. For instance, you now get a count() method that claims that it
returns the number of items in the model. What does that mean in the
context of a tree model?
I added the property (and the new get() method) to QAbstractListModel,
not to QAbstractItemModel.

[...]
Post by André Somers
Perhaps an alternative approach would be to create a proxy model for
exporting to QML. It could include all the methods you need, but it
would not complicate QAIM itself.
[...]

That might be the best approach, yes. But I wonder if we should target
tree models or just list models. The API for tree models might be
significantly more complex (exporting QModelIndex to QML might not be
trivial).

Ciao,
Alberto
André Somers
2013-01-14 10:37:48 UTC
Permalink
Post by Alberto Mardegan
Post by André Somers
I am not fan of this change. I think the API of QAIM is already very
complex. Adding more methods that basically only sort-of mirror existing
methods but for a more confined use-cases only makes that situation
worse. For instance, you now get a count() method that claims that it
returns the number of items in the model. What does that mean in the
context of a tree model?
I added the property (and the new get() method) to QAbstractListModel,
not to QAbstractItemModel.
I am sorry, I overlooked that.

Still, I think the argument is still largely valid: you're introducing
double API into an already complex (some may say: overly complex) API.
And, if the code is in QAbstractListModel, you can still only export
models based on that to QML and rely on these properties to be
available. Is that really useful? Wouldn't table models need to be
exported too, even if you could only get the first column that way?
Event tree models might be useful to display in a flat list. At least, I
have found myself displaying a tree-type model in a QListView on more
than one occasion, thus ignoring all further columns and child nodes.
Post by Alberto Mardegan
[...]
Post by André Somers
Perhaps an alternative approach would be to create a proxy model for
exporting to QML. It could include all the methods you need, but it
would not complicate QAIM itself.
[...]
That might be the best approach, yes. But I wonder if we should target
tree models or just list models. The API for tree models might be
significantly more complex (exporting QModelIndex to QML might not be
trivial).
No, I don't think it would be trivial to create such a mapping. But I
don't think it would be needed to be a useful helper class to get models
into QML.

Even if it only works for the top level nodes of trees, it would still
be (IMHO) a solution that is preferable to your solution. It would work
for all other models, including things like lists that have a QSFPM on
top of them. Note that you can also use a proxy to map a table model to
a list by mapping the data in columns to different roles. The base class
would not be a QListModel, but the data would be available from the
first column anyway. When using the proxy approach I suggest, that works
nicely. Your approach only works for models that derive from
QAbstractListModel directly.

André
Alberto Mardegan
2013-01-14 11:37:55 UTC
Permalink
On 01/14/2013 11:37 AM, André Somers wrote:
[...]
Post by André Somers
top of them. Note that you can also use a proxy to map a table model to
a list by mapping the data in columns to different roles. The base class
would not be a QListModel, but the data would be available from the
first column anyway. When using the proxy approach I suggest, that works
nicely. Your approach only works for models that derive from
QAbstractListModel directly.
Very good point. Maybe this helper class could help expose more complex
models to QML as a list model, by providing a sub-selection of the data.
Something like:

============
ItemModel {
id: itemModel
model: myModel
column: 4
}

ListView {
model: itemModel
}
============

where "myModel" could be a tabular QAbstractItemModel, and in
"itemModel" we select the 4th column in order to create a list model.
Though whether it's useful, it's another matter. :-)

Ciao,
Alberto
Alan Alpert
2013-01-14 15:48:18 UTC
Permalink
On Mon, Jan 14, 2013 at 3:37 AM, Alberto Mardegan
Post by Alberto Mardegan
[...]
Post by André Somers
top of them. Note that you can also use a proxy to map a table model to
a list by mapping the data in columns to different roles. The base class
would not be a QListModel, but the data would be available from the
first column anyway. When using the proxy approach I suggest, that works
nicely. Your approach only works for models that derive from
QAbstractListModel directly.
Very good point. Maybe this helper class could help expose more complex
models to QML as a list model, by providing a sub-selection of the data.
============
ItemModel {
id: itemModel
model: myModel
column: 4
}
ListView {
model: itemModel
}
============
where "myModel" could be a tabular QAbstractItemModel, and in
"itemModel" we select the 4th column in order to create a list model.
Though whether it's useful, it's another matter. :-)
In terms of what's useful, this change was originally inspired by
multiple people saying they augmented an actual list, not a tree or
table model, because they needed to right now. To properly fix all
QAIM usage in QML would be even better, but the last time we looked at
that was the ItemViewsNG project maybe two years ago. I'm still of the
opinion that the proper fix needs a new modelview framework - which
means it won't happen for quite a while. Perhaps it's worth adding a
couple simple methods (count and get specifically) now to ease the
pain of the QML users while they wait?

--
Alan Alpert

Loading...