Results 1 to 3 of 3

Thread: QTreeView / Model problem - rowCount never called with valid parent

  1. #1
    Join Date
    Sep 2020
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question QTreeView / Model problem - rowCount never called with valid parent

    Hi
    've got created an own item model based on QAbstractTableModel.
    Internal data forms a tree structure with multiple top level items.
    The connected QTreeView widget shows all my root level items fine, but NO CHILDS (not at least "+" indicator).

    I get NO CALL FOR rowCount() with a parent index other than invalid (root).

    I expected Qt would call rowCount for all items to check if there are child.
    What do I wrong or what is the misunderstanding?

    Hope for some hints. Thx

    Here is the important part of my model (data() is also implemented but not shown here).

    Qt Code:
    1. QModelIndex HistoryQueryResultModel::index(int row, int column, const QModelIndex &parent) const {
    2. if (!hasIndex(row, column, parent)) {
    3. return QModelIndex();
    4. }
    5.  
    6. HistoryQueryResultModelEntry* item;
    7.  
    8. if (!parent.isValid()) {
    9. // unformtunately index() is const, and createIndex() uses non-const internal pointers to members, which become const because of index() is const
    10. // see https://development.qt-project.narkive.com/r2Uu03QQ/qabstractitemmodel-createindex-void-const
    11. item = const_cast<HistoryQueryResultModelEntry*>(m_entries[row]);
    12. } else {
    13. // unformtunately index() is const, and createIndex() uses non-const internal pointers to members, which become const because of index() is const
    14. item = const_cast<HistoryQueryResultModelEntry*>(static_cast<HistoryQueryResultModelEntry*>(parent.internalPointer())->child(row));
    15. }
    16. if (item) {
    17. return createIndex(row, column, item);
    18. } else {
    19. qt_assert("no item found", __FILE__, __LINE__);
    20. return QModelIndex();
    21. }
    22. }
    23.  
    24. QModelIndex HistoryQueryResultModel::parent(const QModelIndex &index) const {
    25. if (!index.isValid()) {
    26. qt_assert("invalid index", __FILE__, __LINE__);
    27. return QModelIndex();
    28. }
    29.  
    30. HistoryQueryResultModelEntry *childItem = static_cast<HistoryQueryResultModelEntry*>(index.internalPointer());
    31. HistoryQueryResultModelEntry *parentItem = childItem->parentItem();
    32.  
    33. if (!parentItem) {
    34. // we are on root element
    35. return QModelIndex();
    36. }
    37. return createIndex(parentItem->index(), 0, parentItem);
    38. }
    39.  
    40. int HistoryQueryResultModel::rowCount(const QModelIndex &parent) const
    41. {
    42. if (!parent.isValid()) {
    43. return m_entries.size();
    44. } else {
    45. qDebug("call for childs");
    46. HistoryQueryResultModelEntry *childItem = static_cast<HistoryQueryResultModelEntry*>(parent.internalPointer());
    47. if (!childItem) {
    48. qt_assert("invalid internal pointer for child", __FILE__, __LINE__);
    49. }
    50. return childItem->childCount();
    51. }
    52. }
    53.  
    54. int HistoryQueryResultModel::columnCount(const QModelIndex & parent) const
    55. {
    56. // invalid parent means m_entries[row].columns...
    57. return 4;
    58. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2020
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView / Model problem - rowCount never called with valid parent

    Found the problem!
    I inherit from QAbstractTableModel using it with a treeView as well. BUT the QAbstractTableModel overrides hasChildren() and sets NeverHasChild flags for indices for performance.
    I need to inherit from QAbstractItemModel.
    Small thing - big repercussion.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,689
    Thanks
    257
    Thanked 750 Times in 740 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QTreeView / Model problem - rowCount never called with valid parent

    inherit from QAbstractTableModel using it with a treeView as well.
    A table is not a tree. Table model rows have neither parents nor children.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 29th August 2013, 06:41
  2. moveCursor not called in a QTreeView
    By mathieuS in forum Qt Programming
    Replies: 0
    Last Post: 22nd July 2011, 22:26
  3. problem with rowCount method
    By sergio486 in forum Qt Programming
    Replies: 2
    Last Post: 18th December 2010, 09:37
  4. rowCount and model
    By steg90 in forum Newbie
    Replies: 3
    Last Post: 11th May 2007, 10:14

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.