Results 1 to 5 of 5

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

Threaded View

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

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

    Hi all,

    First post here - I'm beginning on Qt, having just gotten a good foothold [though I suppose these things are always subjective] on C++. Here's my issue:

    I'm creating a QTreeView hooked up to a custom model that stores file paths for the program to process, sorted by "Device" (or some other arbitrary header). If the program gets passed a valid file path, it should automatically parent it to a new device.

    So, the idea is to have:
    Root Item
    |
    - Device Item #1
    - File #1
    - File #2
    - ....
    - Device Item #2
    [/code]

    In the data structure, everything seems fine. Things are constructed appropriately, in Debug mode, I can walk through from the root item all the way to each child through the recursive layers of QList<Node *>. All the data seems correct.

    When I hook it up to the QTreeView, and try to expand, I end up with the DisplayRole data of the parent being displayed for all the children. I've tried variations with deeper levels of children/parents, but it always shows me incorrect data for the children. In what seems like adding insult to injury, the WhatsThisRole data actually displays correctly - I can hover over the item and it will show me the correct text (it showing me the correct text was an addition by me in the data() function just to make sure I wasn't going crazy). Adding to the confusion, it appears that the selection model seems to be off a bit - I can't unclick items once selected, and selecting a child item selects all child items, same parent or not.

    It has to be something in my code. I have a bunch of logging/debugging lines set into it that will take a bit to back out (without losing them). I'm assuming though that the issue is in my Model implementation. Being my first time with this, I'm heavily leaning on the examples provided in books and online to get a good model implementation going. Code below.

    Qt Code:
    1. QModelIndex ParseList::index(int row, int column, const QModelIndex &parent) const
    2. {
    3. if(!head || row < 0 || column < 0)
    4. {
    5. return QModelIndex();
    6. }
    7. ParseNode * parentNode = nodeFromIndex(parent);
    8. ParseNode * childNode = parentNode->getChild(row);
    9. if(!childNode)
    10. {
    11. return QModelIndex();
    12. }
    13. QModelIndex temp = createIndex(row,column, childNode);
    14. return createIndex(row, column, childNode);
    15. }
    16.  
    17. QModelIndex ParseList::parent(const QModelIndex &child) const
    18. {
    19. ParseNode * node = nodeFromIndex(child);
    20. if(!node)
    21. return QModelIndex();
    22. ParseNode * parentNode = node->getParent();
    23. if(!parentNode)
    24. {
    25. return QModelIndex();
    26. }
    27. ParseNode * grandparentNode = parentNode->getParent();
    28. if(!grandparentNode)
    29. {
    30. return QModelIndex();
    31. }
    32. int row = grandparentNode->indexOf(parentNode);
    33. return createIndex(row, 0, parentNode);
    34. }
    35.  
    36. int ParseList::rowCount(const QModelIndex &parent) const
    37. {
    38. if (parent.column() > 0)
    39. {
    40. return 0;
    41. }
    42. ParseNode *parentNode = nodeFromIndex(parent);
    43. if(!parentNode)
    44. {
    45. return 0;
    46. }
    47. return parentNode->childCount();
    48. }
    49.  
    50. int ParseList::columnCount(const QModelIndex &parent) const
    51. {
    52. return ParseNode::dataColumns;
    53. }
    54.  
    55. QVariant ParseList::data(const QModelIndex &index, int role) const
    56. {
    57. if(!index.isValid())
    58. {
    59. return QVariant();
    60. }
    61. ParseNode * node = nodeFromIndex(index);
    62. if (!node)
    63. {
    64. return QVariant();
    65. }
    66. //TODO: Decide if smaller functions would be better
    67. // - small private functions
    68.  
    69. switch(index.column())
    70. {
    71. case 0:
    72. switch(role)
    73. {
    74. case Qt::DisplayRole:
    75. {
    76. return node->getPathString(); //stored as QString in node
    77. }
    78. case Qt::WhatsThisRole:
    79. case Qt::StatusTipRole:
    80. case Qt::ToolTipRole:
    81. return node->getPathString();//tr("Path of selected log"); //Added for debug
    82. default:
    83. return QVariant();
    84. }
    85. case 1:
    86. switch(role)
    87. {
    88. case Qt::DisplayRole:
    89. return node->getParseStatus() ? tr("Yes") : tr("No"); //Stored as bool in underlying class
    90. case Qt::WhatsThisRole:
    91. case Qt::StatusTipRole:
    92. case Qt::ToolTipRole:
    93. return node->getParseStatus() ?
    94. tr("Log has been parsed") :
    95. tr("Log has not been parsed");
    96. default:
    97. return QVariant();
    98. }
    99. case 2:
    100. switch(role)
    101. {
    102. case Qt::DisplayRole:
    103. switch(node->getPathType()) //public enum declared by ParseNode
    104. {
    105. case ParseNode::File:
    106. return tr("File");
    107. case ParseNode::FileButNotReadable:
    108. return tr("File - Unreadable");
    109. case ParseNode::Directory:
    110. return tr("Directory");
    111. case ParseNode::Invalid:
    112. return tr("Invalid");
    113. case ParseNode::Device:
    114. return tr("Device");
    115. default:
    116. return QVariant();
    117. }
    118. case Qt::WhatsThisRole:
    119. case Qt::StatusTipRole:
    120. case Qt::ToolTipRole:
    121. switch(node->getPathType())
    122. {
    123. case ParseNode::File:
    124. return tr("File from system");
    125. case ParseNode::FileButNotReadable:
    126. return tr("File exists, but is unreadable");
    127. case ParseNode::Directory:
    128. return tr("Directory from system");
    129. case ParseNode::Invalid:
    130. return tr("Invalid path specified");
    131. default:
    132. return QVariant();
    133. }
    134. default:
    135. return QVariant();
    136. }
    137. default:
    138. return QVariant();
    139. }
    140. }
    To copy to clipboard, switch view to plain text mode 

    And here's a screencap with things hooked up and at their worst:
    qtreeview1.jpg

    When I take out the "auto-reparenting" code, everything displays correctly on the top level, but again, all the children display the parent's data.

    Hopefully I've provided a good start of information - does anyone have any ideas? I can work on trimming things up further to get a .zip of what I'm seeing together as a compilable example.

    I'm using Qt 4.8.1 with MinGW as a compiler. The IDE I'm using is Qt Creator 2.4.1.

    Thanks in advance!

  2. The following user says thank you to vulcan 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, 09:51
  2. Replies: 0
    Last Post: 14th April 2010, 15:03
  3. disable a parent and children in a tree model
    By qt_gotcha in forum Newbie
    Replies: 4
    Last Post: 9th July 2009, 06:49
  4. Process the display data before displaying in QTreeView
    By babu198649 in forum Qt Programming
    Replies: 5
    Last Post: 23rd June 2009, 05: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.