Results 1 to 16 of 16

Thread: How to implement nested editable lists in C++ as models in Qt (QML oriented)

  1. #1
    Join Date
    Nov 2014
    Posts
    20
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default 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?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. #3
    Join Date
    Nov 2014
    Posts
    20
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default 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?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2014
    Posts
    20
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)

    Quote Originally Posted by wysota View Post
    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?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)

    Quote Originally Posted by ribtoks View Post
    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,
    _

  7. #7
    Join Date
    Nov 2014
    Posts
    20
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)

    Quote Originally Posted by anda_skoa View Post
    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?

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  9. The following user says thank you to anda_skoa for this useful post:

    ribtoks (24th November 2014)

  10. #9
    Join Date
    Nov 2014
    Posts
    20
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)

    Quote Originally Posted by anda_skoa View Post
    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.

  11. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)

    Your own slots. Methods to manipulate your data.

    Cheers,
    _

  12. #11
    Join Date
    Nov 2014
    Posts
    20
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)

    Quote Originally Posted by anda_skoa View Post
    Your own slots. Methods to manipulate your data.
    Ok, I'll read more on that. Sorry for stupid questions.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)

    Quote Originally Posted by ribtoks View Post
    Ok, I tried to implement it as QAbstractListModel,
    Why not QAbstractItemModel?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #13
    Join Date
    Nov 2014
    Posts
    20
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)

    Quote Originally Posted by wysota View Post
    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)

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)

    Quote Originally Posted by wysota View Post
    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 View Post
    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,
    _

  17. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to implement nested editable lists in C++ as models in Qt (QML oriented)

    Quote Originally Posted by anda_skoa View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Nested lists in QT QML
    By Raghavendra R M in forum Qt Quick
    Replies: 13
    Last Post: 14th October 2013, 11:27
  2. Replies: 7
    Last Post: 28th April 2013, 01:46
  3. nested lists in QTextDocumentFragment::toHtml()
    By Al_ in forum Qt Programming
    Replies: 0
    Last Post: 2nd March 2012, 10:16
  4. Editable Models
    By MTK358 in forum Newbie
    Replies: 1
    Last Post: 9th September 2010, 20:58

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.