I am trying to implement lazy population in a QStandardItemModel derived class. While the documentation is pretty good for doing it in QAbstractItemModel derivations, it seems there are a few gotchas I am missing for the Standard model.

The general approach I am trying is this:

Qt Code:
  1. class QCustomItem : public QStandardItem
  2. {
  3. /**
  4. * Returns the number of children the item can create.
  5. */
  6. virtual unsigned int countChildren() const = 0;
  7.  
  8. /**
  9. * Actually appends the children to the item.
  10. */
  11. virtual void appendChildren() = 0;
  12. };
  13.  
  14. class QCustomModel : public QStandardItemModel
  15. {
  16. virtual bool canFetchMore(const QModelIndex &Index) const
  17. {
  18. if ( false == Index.isValid() )
  19. {
  20. // Must be asking about children to the invisible root item.
  21. // Since model has root folders, return true.
  22. return true;
  23. }
  24. else
  25. {
  26. QCustomItem* cusItem = dynamic_cast<QCustomItem*>itemFromIndex(Index);
  27.  
  28. if ( NULL != cusItem )
  29. return ( 0 < cusItem->countChildren() );
  30. else
  31. return false;
  32. }
  33. }
  34.  
  35. virtual bool hasChildren(const QModelIndex& parent ) const
  36. {
  37. QCustomItem* cusItem = dynamic_cast<QCustomItem*>itemFromIndex(Index);
  38.  
  39. if ( NULL != cusItem )
  40. return cusItem->hasChildren();
  41. else
  42. return false;
  43. }
  44.  
  45. void fetchMore(const QModelIndex& parent)
  46. {
  47. if ( false == Index.isValid() )
  48. {
  49. // add consistant root folders.
  50. }
  51.  
  52. QCustomItem* cusItem = dynamic_cast<QCustomItem*>itemFromIndex(Index);
  53.  
  54. if ( NULL != cusItem )
  55. cusItem->appendChildren();
  56. }
  57. };
To copy to clipboard, switch view to plain text mode 

When I debug the code, these fucntions behave as I expect, but using a QTreeView, I am not able to expand items when should be registering as having children, just not yet populated. However, population of root items works. Also, if after the fact the data changes to change the "hasChildren" status of an item, how do I alert the data model (and thus the views), to reexamine the item and add or remove the sub-folder plus/arrow control beside the item in the view?

Thanks for any assistance.