Results 1 to 5 of 5

Thread: QTreeView - Children Displaying Same Data As Parent, WhatsThisRole is Not

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2013
    Location
    Wisconsin, USA
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows
    Thanked 3 Times in 3 Posts

    Question Re: QTreeView - Children Displaying Same Data As Parent, WhatsThisRole is Not

    Does anyone have any ideas? I'm stuck going in circles and can't seem to find where the issue is. Is there any additional information I can provide, or perhaps things I can look for?

    Thanks!

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


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

    Default Re: QTreeView - Children Displaying Same Data As Parent, WhatsThisRole is Not

    This is surely invalid:

    Qt Code:
    1. void ParseNode::addChild(ParseNode *child)
    2. {
    3. if(child && children.indexOf(child) == 0); // <--- semicolon here?
    4. children.push_back(child);
    5. }
    To copy to clipboard, switch view to plain text mode 

    In my opinion it should be:

    Qt Code:
    1. void ParseNode::addChild(ParseNode *child)
    2. {
    3. if(child && children.indexOf(child) < 0) {
    4. children.push_back(child);
    5. child->myParent = this;
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    removeChild() has to be adjusted accordingly.

    ParseList::addPaths() looks invalid as well.... You're (incorrectly) informing the model that you'll be adding top-level items but then you're never attaching items you're adding to the root node (you only set it as the new item's parent but never add it to the root node's list of children thus parent() implementation can't work correctly). Maybe there are other problems as well, I can't see them right now.

    A minimal tree implementation looks more or less like this:

    Qt Code:
    1. #include <QApplication>
    2. #include <QAbstractItemModel>
    3. #include <QTreeView>
    4.  
    5. template<typename T> class Node {
    6. public:
    7. Node() { parentNode = 0; }
    8. ~Node() {
    9. if(parentNode)
    10. parentNode->removeChild(this);
    11. qDeleteAll(children);
    12. }
    13. void addChild(Node *n) {
    14. if(n->parentNode) {
    15. if(n->parentNode == this) return;
    16. n->parentNode->removeChild(n);
    17. n->parentNode = 0;
    18. }
    19. n->parentNode = this;
    20. children.append(n);
    21. }
    22. void removeChild(Node *n) {
    23. if(n->parentNode != this) return;
    24. n->parentNode = 0;
    25. children.removeOne(n);
    26. }
    27. int indexOf(Node *n) const { return children.indexOf(n); }
    28. Node *at(int idx) const { return children.at(idx); }
    29. int count() const { return children.count(); }
    30. Node *parent() const { return parentNode; }
    31. T data;
    32.  
    33. private:
    34. Node<T> *parentNode;
    35. QList<Node<T>*> children;
    36. };
    37.  
    38. template<typename T, int ColCount> class TreeModel : public QAbstractItemModel {
    39. public:
    40. TreeModel(QObject *parent = 0) : QAbstractItemModel(parent) {
    41. root = new Node<T>();
    42. }
    43. ~TreeModel() { delete root; }
    44. int rowCount(const QModelIndex &parent = QModelIndex()) const {
    45. return nodeFromIndex(parent)->count();
    46. }
    47. int columnCount(const QModelIndex &parent = QModelIndex()) const {
    48. return ColCount;
    49. }
    50. QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const {
    51. Node<T> *parentNode = nodeFromIndex(parent);
    52. if(row < 0 || row >= parentNode->count() || column < 0 || column >= ColCount) return QModelIndex();
    53. return createIndex(row, column, parentNode->at(row));
    54. }
    55. QModelIndex parent(const QModelIndex &child) const {
    56. Node<T> *childNode = nodeFromIndex(child);
    57. Node<T> *parentNode = childNode->parent();
    58. if(parentNode == root) return QModelIndex();
    59. Node<T> *grandparentNode = parentNode->parent();
    60. return createIndex(grandparentNode->indexOf(parentNode), 0, parentNode);
    61. }
    62. QVariant data(const QModelIndex &index, int role) const {
    63. qWarning("Reimplement in subclass");
    64. return QVariant();
    65. }
    66. protected:
    67. Node<T>* nodeFromIndex(const QModelIndex &idx) const {
    68. if(idx.isValid()) return static_cast<Node<T>*>(idx.internalPointer());
    69. return root;
    70. }
    71. Node<T> *root;
    72. };
    73.  
    74. class Model : public TreeModel<int, 1> {
    75. public:
    76. Model(QObject *parent = 0) : TreeModel(parent) {}
    77. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const {
    78. if(index.column() != 0) return QVariant();
    79. if(role != Qt::DisplayRole && role != Qt::EditRole) return QVariant();
    80. Node<int> *n = nodeFromIndex(index);
    81. return n->data;
    82. }
    83. bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) {
    84. if(index.column() != 0) return false;
    85. if(role != Qt::DisplayRole && role != Qt::EditRole) return false;
    86. Node<int> *n = nodeFromIndex(index);
    87. if(n->data != value.toInt()) {
    88. n->data = value.toInt();
    89. emit dataChanged(index, index);
    90. return true;
    91. }
    92. return true;
    93. }
    94. void addItem(int dat) {
    95. beginInsertRows(QModelIndex(), root->count(), root->count());
    96. Node<int> *n = new Node<int>();
    97. n->data = dat;
    98. Node<int> *sn = new Node<int>();
    99. sn->data = dat*10;
    100. n->addChild(sn);
    101. root->addChild(n);
    102. endInsertRows();
    103. }
    104. };
    105.  
    106. class Adder : public QObject {
    107. public:
    108. Adder(Model *model, int interval = 2000) {
    109. m_model = model;
    110. startTimer(interval);
    111. }
    112. protected:
    113. void timerEvent(QTimerEvent *) {
    114. int val = rand() % 100;
    115. m_model->addItem(val);
    116. }
    117.  
    118. private:
    119. Model *m_model;
    120. };
    121.  
    122. int main(int argc, char *argv[])
    123. {
    124. QApplication a(argc, argv);
    125. Model model;
    126. // TreeModel<int,1> model;
    127. w.setModel(&model);
    128. w.show();
    129. Adder adder(&model, 1000);
    130. return a.exec();
    131. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 22nd January 2013 at 21:24.
    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.


  4. The following user says thank you to wysota for this useful post:


Similar Threads

  1. Hide Parent and show only children in QTreeView
    By y.s.bisht in forum Qt Programming
    Replies: 8
    Last Post: 19th January 2012, 10:51
  2. Replies: 0
    Last Post: 14th April 2010, 16:03
  3. disable a parent and children in a tree model
    By qt_gotcha in forum Newbie
    Replies: 4
    Last Post: 9th July 2009, 07:49
  4. Process the display data before displaying in QTreeView
    By babu198649 in forum Qt Programming
    Replies: 5
    Last Post: 23rd June 2009, 06:21

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