Results 1 to 9 of 9

Thread: Tree model implementation for C++ data to QML

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #9
    Join Date
    Jan 2021
    Posts
    5
    Thanks
    3

    Default Re: Tree model implementation for C++ data to QML

    So I was able to get a prototype working that looks something like this (warning: pretty far from the implementation idea discussed so far). This prototype captures the simplicity of how I "thought" things should be able to work, so at least now I don't feel crazy. I by no means think this is the right or best way though, but that is mentioned at the bottom of this post

    Qt Code:
    1. class Person : public QObject {
    2. Q_OBJECT
    3. Q_PROPERTY(QString name MEMBER m_name)
    4. Q_PROPERTY(int age MEMBER m_age)
    5. public:
    6. explicit Person(Group* parent = nullptr);
    7. explicit Person(const QString& name, int age, Group* parent = nullptr);
    8.  
    9. private:
    10. QString m_name = "";
    11. int m_age;
    12. };
    13.  
    14. class Group : public QObject {
    15. Q_OBJECT
    16. Q_PROPERTY(QString nickname MEMBER m_nickname)
    17. public:
    18. explicit Group(Classroom* parent = nullptr);
    19. explicit Group(QString nickname, Classroom* parent = nullptr);
    20. ~Group();
    21.  
    22. void appendPerson(Person* person);
    23. Q_INVOKABLE QObject* person(int index);
    24. int personCount() const;
    25. signals:
    26. void personCountChanged();
    27.  
    28. private:
    29. QString m_nickname;
    30. QList<Person*> m_people;
    31. };
    32.  
    33. class Classroom: public QObject {
    34. Q_OBJECT
    35. Q_PROPERTY(int groupCount READ groupCount NOTIFY groupCountChanged)
    36. public:
    37. Classroom(QObject* parent = nullptr);
    38. ~Classroom();
    39.  
    40. void appendGroup(Group* group);
    41.  
    42. Q_INVOKABLE QObject* getGroup(int index) const;
    43. int groupCount() const;
    44. signals:
    45. void groupCountChanged();
    46.  
    47. private:
    48. // Temp debug code to populate the model
    49. void populateModel();
    50.  
    51. QList<Group*> m_groups;
    52. };
    To copy to clipboard, switch view to plain text mode 

    and then in QML I am able to access it with stuff like this:

    Qt Code:
    1. console.log(ClassroomModel.getGroup(groupNum).person(1).name);
    To copy to clipboard, switch view to plain text mode 

    or with a property

    Qt Code:
    1. property int groupCount: ClassroomModel.groupCount
    To copy to clipboard, switch view to plain text mode 

    I think I understand the tradeoffs between using this vs subclassing a qabstractmodel, but wanted to put my thoughts on these explicitly incase there is something I am missing

    1) My datastructures are QObjects, not raw clean c++/stl, so a translation layer will be needed if I want to use raw stl structures on the backend to populate the model
    2) I wrote a custom interface, and my view code will need to be updated more often than if I were to have used the interface from qabstractmodel
    3) I can put in notify signals wherever I want really at any layer
    4) Performance as you said doesn't seem to be bad to do a complete destroy/recreate of my view with the number of items I am talking, but there is room here to optimize using some smarter notify/change signals
    5) Not compatible with tableview/etc as is, but can be used for Repeater counts and anything else where an exposed property would suffice
    6) I can even use this https://doc.qt.io/qt-5/qtqml-cppinte...ect-list-types to simplify the API even more

    This looks like it is going to work for my application, but I would love to hear about any counterpoints (either the same as what I identified above, or different/new)

    I was really struggling with modelindex and how it could work for me, but that could always be a refactor/change I tackle in the future.
    Last edited by xconverge; 24th January 2021 at 23:29.

Similar Threads

  1. Replies: 3
    Last Post: 17th January 2016, 18:06
  2. Replies: 3
    Last Post: 16th November 2015, 21:22
  3. How to map tree model data to list view
    By msopanen in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2009, 19:56
  4. Replies: 1
    Last Post: 7th July 2009, 07:13
  5. data, model and tree view
    By larry104 in forum Qt Programming
    Replies: 17
    Last Post: 3rd July 2006, 14:43

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.