Results 1 to 20 of 54

Thread: Integration: Nested Tree Parent Child C++ Classes with Models/Delegates for QML

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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: Integration: Nested Tree Parent Child C++ Classes with Models/Delegates for QML

    Just sketching some ideas here:

    Qt Code:
    1. class Card
    2. {
    3. QString id;
    4. QString name;
    5. // ....
    6. };
    7.  
    8. class CardManager : public QObject
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. // returns an invalid card if ID is not found, e.g. Card object with empty "id"
    14. Card addCardById(const QString &id);
    15. Card addCardByName(const QString &name);
    16.  
    17. Card card(const QString &id) const;
    18. QList<Card> cards() const;
    19.  
    20. int count() const;
    21.  
    22. signals:
    23. void cardAdded(const QString &id);
    24. void cardRemoved(const QString &id);
    25.  
    26. private:
    27. QList<Card> m_cards;
    28. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class Album
    2. {
    3. QString id;
    4. QString name;
    5. QStringList cardIDs;
    6. };
    7.  
    8. class AlbumManager : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. Album createAlbum(const QString &name);
    13.  
    14. Album album(const QString &id) const;
    15.  
    16. QList<Album> albums() const;
    17.  
    18. int count() const;
    19.  
    20. void addCard(const QString &albumId, const QString &cardId);
    21. void removeCard(const QString &albumId, const QString &cardId);
    22.  
    23. signals:
    24. void albumAdded(const QString &id);
    25. void albumRemoved(const QString &id);
    26.  
    27. void cardAdded(const QString &albumId, const QStringList &cardIDs);
    28. void cardRemoved(const QString &albumId, const QStringList &cardIDs);
    29. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class AbstractCardModel : public QAbstractListModel
    2. {
    3. Q_OBJECT
    4. public:
    5. enum Roles {
    6. IdRole = Qt::UserRole + 1,
    7. NameRole,
    8. // ....
    9. }
    10.  
    11. explicit AbstractCardModel(CardManager *cardManager, QObject *parent = 0);
    12.  
    13. // this uses cardForIndex to get the card, then uses role to return the actual data
    14. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    15.  
    16. protected:
    17. QHash<int, QByteArray> roleNames() const;
    18.  
    19. virtual Card cardForIndex(const QModelIndex &index) const = 0;
    20.  
    21. protected:
    22. CardManager *m_cardManager;
    23. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // for showing the cards of a single album
    2. class AlbumCardsModel : public AbstractCardsModel
    3. {
    4. Q_OBJECT
    5. public:
    6. AlbumCardsModel(AlbumManager *albumManager, CardManager *cardManager, QObject *parent = 0);
    7.  
    8. void showAlbum(const QString &albumId);
    9.  
    10. int rowCount(const QModelIndex &parent) const;
    11.  
    12. protected:
    13. Card cardForIndex(const QModelIndex &index) const;
    14.  
    15. private slots:
    16. // gets connected to m_albumManager's albumRemoved() signal
    17. void onAlbumRemoved(const QString &albumId);
    18.  
    19. void onCardAdded(const QString &albumId, const QString cardId);
    20. void onCardRemoved(const QString &albumId, const QString cardId);
    21.  
    22. private:
    23. AlbumManager *m_albumManager;
    24. Album m_album;
    25. };
    26.  
    27. void AlbumCardsModel::showAlbum(const QString &albumId)
    28. {
    29. beginResetModel();
    30.  
    31. m_album = m_albumManager->album(albumId);
    32.  
    33. endResetModel();
    34. }
    35.  
    36. int AlbumCardsModel::rowCount(const QModelIndex&) const
    37. {
    38. return m_album.cardIDs.count();
    39. }
    40.  
    41. Card AlbumCardsModel::cardForIndex(const QModelIndex &index) const
    42. {
    43. return m_cardManager->card(m_album.cardIDs.at(index.row());
    44. }
    45.  
    46. void AlbumCardsModel::onAlbumRemoved(const QString &albumId)
    47. {
    48. if (albumId != m_album.id) return;
    49.  
    50. beginResetModel();
    51. m_album = Album();
    52. endResetModel();
    53. }
    54.  
    55. void AlbumCardsModel::onCardAdded(const QString &albumId, const QString &cardId)
    56. {
    57. if (albumId != m_album.id) return;
    58.  
    59. const Album updatedAlbum = m_albumManager->album(albumId);
    60. const int row = updatedAlbum.cardIDs.indexOf(cardId);
    61.  
    62. beginInsertRows(QModelIndex(), row, row);
    63.  
    64. m_album = updatedAlbum;
    65.  
    66. endInsertRows();
    67. }
    68.  
    69. void AlbumCardsModel::onCardRemoved(const QString &albumId, const QString &cardId)
    70. {
    71. if (albumId != m_album.id) return;
    72.  
    73. const Album updatedAlbum = m_albumManager->album(albumId);
    74. const int row = m_album.cardIDs.indexOf(cardId);
    75.  
    76. beginRemoveRows(QModelIndex(), row, row);
    77.  
    78. m_album = updatedAlbum;
    79.  
    80. endRemoveRows();
    81. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  2. The following 2 users say thank you to anda_skoa for this useful post:

    d_stranz (30th December 2016), Nizars (30th December 2016)

Similar Threads

  1. Nested Models
    By Dan7 in forum Qt Programming
    Replies: 1
    Last Post: 26th August 2015, 20:31
  2. Models and Delegates
    By bgeller in forum Newbie
    Replies: 13
    Last Post: 4th March 2010, 04:46
  3. QStandardItemModel, parent / child to form tree structure
    By Nightfox in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2010, 17:01
  4. Nested delegates in a QListWidget
    By youkai in forum Qt Programming
    Replies: 9
    Last Post: 7th April 2009, 08:48
  5. Models, delegates or views and how?
    By maddogg in forum Newbie
    Replies: 3
    Last Post: 9th November 2007, 13:59

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.