Results 1 to 13 of 13

Thread: QTreeView display of graph (multiple parents of item)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 1 Time in 1 Post

    Default Re: QTreeView display of graph (multiple parents of item)

    I know this thread is too old, but maybe my findings will be useful.

    I also wanted to have multiple parents for my qtreeview entities (items) and came up with the following solution.

    I have an IndexData structure that contains information about current item and it's parent:
    Qt Code:
    1. struct IndexData{
    2. IndexData *parentIndex;
    3. Entity *parent;
    4. Entity *item;
    5. int row;
    6. };
    To copy to clipboard, switch view to plain text mode 

    When QAbstractItemModel::createIndex() function is called, I pass a pointer to IndexData structure as the third parameter.

    Full item model class is the following (Please note that it is a quick and dirty solution with memory leaks that was not well tested):
    Qt Code:
    1. #include "entitymodel.h"
    2. #include <QDebug>
    3.  
    4. EntityModel::EntityModel(Entity *root, QObject *parent) :
    5. {
    6. m_root = root;
    7. m_rootIndexData = new IndexData;
    8. m_rootIndexData->parent = 0;
    9. m_rootIndexData->parentIndex = 0;
    10. m_rootIndexData->row = 0;
    11. m_rootIndexData->item = root;
    12. }
    13.  
    14. QModelIndex EntityModel::index(int row, int column, const QModelIndex &parent) const
    15. {
    16. if (!hasIndex(row, column, parent)) {
    17. return QModelIndex();
    18. }
    19.  
    20. IndexData *parentData = indexData(parent);
    21. Entity *child = parentData->item->child(row);
    22.  
    23. if (child)
    24. return createIndex(row, column, createIndexData(parentData, child, row));
    25.  
    26. return QModelIndex();
    27. }
    28.  
    29. QModelIndex EntityModel::parent(const QModelIndex &child) const
    30. {
    31. if (!child.isValid())
    32. return QModelIndex();
    33.  
    34. IndexData *data = indexData(child);
    35. if (data->parent == m_root) {
    36. return QModelIndex();
    37. }
    38.  
    39. if (data->parentIndex == 0)
    40. return QModelIndex();
    41.  
    42. return createIndex(data->parentIndex->row, 0, data->parentIndex);
    43. }
    44.  
    45. int EntityModel::rowCount(const QModelIndex &parent) const
    46. {
    47. IndexData *data = indexData(parent);
    48. return data->item->childrenCount();
    49. }
    50.  
    51. int EntityModel::columnCount(const QModelIndex &parent) const
    52. {
    53. return 2;
    54. }
    55.  
    56. QVariant EntityModel::data(const QModelIndex &index, int role) const
    57. {
    58. if (!index.isValid())
    59. return QVariant();
    60.  
    61. if (role != Qt::DisplayRole)
    62. return QVariant();
    63.  
    64. Entity *item = indexData(index)->item;
    65.  
    66. if (index.column() == 0) {
    67. return item->name();
    68. } else if (index.column() == 1) {
    69. return "Entity";
    70. } else {
    71. qCritical() << "Invalid column";
    72. return "";
    73. }
    74. }
    75.  
    76. EntityModel::IndexData *EntityModel::indexData(const QModelIndex &index) const
    77. {
    78. if (index.isValid()) {
    79. IndexData *data = static_cast<IndexData*>(index.internalPointer());
    80. if (data)
    81. return data;
    82. }
    83.  
    84. return m_rootIndexData;
    85. }
    86.  
    87. EntityModel::IndexData *EntityModel::createIndexData(IndexData *parent, Entity *item, int row) const
    88. {
    89. if (!m_indexData.contains(quintptr(parent->item))) {
    90. QHash<quintptr, IndexData*>* hash = new QHash<quintptr, IndexData*>();
    91. m_indexData.insert(quintptr(parent->item), hash);
    92. }
    93.  
    94. if (!m_indexData[quintptr(parent->item)]->contains(quintptr(item))) {
    95. IndexData *data = new IndexData;
    96. data->parent = parent->item;
    97. data->item = item;
    98. data->parentIndex = parent;
    99. data->row = row;
    100. m_indexData[quintptr(parent->item)]->insert(quintptr(item), data);
    101. }
    102.  
    103. return m_indexData[quintptr(parent->item)]->value(quintptr(item));
    104. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to Petro Svintsitskyi for this useful post:

    d_stranz (16th May 2014)

  3. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,334
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    317
    Thanked 871 Times in 858 Posts

    Default Re: QTreeView display of graph (multiple parents of item)

    Thanks for this. Now I need to figure out how to implement a model that will handle an undirected cyclic graph which may contain disjoint subgraphs :-) I might have to save that for another lifetime.

Similar Threads

  1. QTreeWidget item with multiple parents
    By di_zou in forum Newbie
    Replies: 0
    Last Post: 16th November 2009, 15:32
  2. Can't change the text display of a QTreeview item.
    By johnny_sparx in forum Qt Programming
    Replies: 3
    Last Post: 2nd June 2006, 01:03

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
  •  
Qt is a trademark of The Qt Company.