How to implement nested editable lists in C++ as models in Qt (QML oriented)
I'm developing an application in QML + C++. I have list of models (say, ParentList) with each item having list of items (say, ChildList). User should be able to add/remove items from ParentList as well as add/remove items in ChildList of each ParentList item.
Which architecture is best suited for it? Can you provide an example?
Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)
Well, depends on your data on each level.
One option could be to have a list of object for the first level, each having the item's data as properties, plus an additional list of bjects property for the children, each again, carrying the data in properties.
Or having the second level as a list model
Or having both levels as list models.
Cheers,
_
Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)
Let's suppose I have List of objects and each of them has a StringList (and we can add/remove items in StringList as well as parent Lists). What I'm asking is how to implement it in C++ correctly to ensure all bindings will work in QML?
Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)
Best of you implement it as a full fledged QAbstractItemModel subclass, expose it to QML and equip with a nice API. Then you can use VisualDataModel to traverse this model easily in the ListView.
Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)
Quote:
Originally Posted by
wysota
Best of you implement it as a full fledged
QAbstractItemModel subclass, expose it to QML and equip with a nice API. Then you can use VisualDataModel to traverse this model easily in the ListView.
Ok, I tried to implement it as QAbstractListModel, but I failed to return inner list in override of data() method as QVariant. What is the correct way to expose inner property (which is a model but not an ordinary property) to QML?
Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)
Quote:
Originally Posted by
ribtoks
Let's suppose I have List of objects and each of them has a StringList (and we can add/remove items in StringList as well as parent Lists).
If your objects are QObject derived then this is trivial: have the list of them as a property on some object or as a context property and their QStringList as a property of each object.
If you objects are not QObject derived, create a simple list model (derive from QAbstractListModel), as wysota suggested.
Cheers,
_
Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)
Quote:
Originally Posted by
anda_skoa
If your objects are QObject derived then this is trivial: have the list of them as a property on some object or as a context property and their QStringList as a property of each object.
If you objects are not QObject derived, create a simple list model (derive from QAbstractListModel), as wysota suggested.
I've read in docs, that I should return that property with list in method data(role) which would return child item's property value with role as a key (property name). data() returns QVariant according to docs. Would it be correct to return QVariant::fromValue(myInnerListHere)? My control needs to provide edit functionality both to parent and child items. If I'll return child's item through QVariant::fromValue() in method data() would all my bindings (which allow to add/remove/edit) work correct?
Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)
QVariant::fromValue() on the stringlist sounds correct, the QML engine should be able to handle that as an array of strings on its side.
For the editing just have some slots or Q_INVOKABLEs in your model class. Don't forget to make the model emit the right signals (e.g. dataChanged() for changes to the string lists)
Cheers,
_
Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)
Quote:
Originally Posted by
anda_skoa
For the editing just have some slots or Q_INVOKABLEs in your model class.
_
Could you please be more specific on that? Which slots? Link to the docs will be ok.
Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)
Your own slots. Methods to manipulate your data.
Cheers,
_
Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)
Quote:
Originally Posted by
anda_skoa
Your own slots. Methods to manipulate your data.
Ok, I'll read more on that. Sorry for stupid questions.
Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)
Quote:
Originally Posted by
ribtoks
Ok, I tried to implement it as QAbstractListModel,
Why not QAbstractItemModel?
Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)
Quote:
Originally Posted by
wysota
Because it looked easier (I hadn't idea why I need QAbstractItemModel). I had class for List of Lists which was QAbstractListModel and child List which also was QAbstractListModel (which I planned to return as a property through parent's data() method).
Why this is bad? (or why this was bad? I'm going to rewrite it based on responses in this thread)
Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)
You want a hierarchical model thus it seems to me implementing a proper model will do the job. I don't think returning a list in each model which will in turn return a list of its own is a better approach.
Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)
Quote:
Originally Posted by
wysota
Easier to implement a list with QAbstractListModel at its base.
QAbstractItemModel requires implementations for columnCount(), index() and parent(), the latter two usually not trivial, and neither is needed for a list.
Quote:
Originally Posted by
wysota
You want a hierarchical model thus it seems to me implementing a proper model will do the job.
Aside from ribtoks data not being hierachical as it turns out, I don't think QtQuick can deal with tree models at this point.
Cheers,
_
Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)
Quote:
Originally Posted by
anda_skoa
I don't think QtQuick can deal with tree models at this point.
It can deal with tree models with the use of VisualDataModel element which allows to set the root index to traverse different layers of the model within the view.