Hi,

I don't want to use tree items for my tree view so I have designed my model as below.

I need to build tree based on 0's & 1's
All zeros are top level items, and 1's followed by 0 are child's for 0 & all 2's followed by 1 are child's of 1.


Qt Code:
  1. class Model : public QAbstractItemModel {
  2.  
  3. Q_OBJECT
  4. public:
  5. //all necessary functions for model
  6. private:
  7. mutable QVector<int> m_vector;
  8. mutable QVector< QVector<int> > childs; // childs for top level items ex: if vector of vectors is [1 1 1] [1 1 1] [1 1 1] -> 1st top level items has 3 child's & 2nd, 3rd top level items also have 3 childs
  9. };
To copy to clipboard, switch view to plain text mode 

So my model is:

Qt Code:
  1. int Model::rowCount(const QModelIndex &parent) const
  2. {
  3. if(!parent.isValid()){
  4. return 5000; // 5000 top level items
  5. }
  6. else if(!parent.parent().isValid()) //limited to 2 levels
  7. {
  8. return childs.at(parent.row()).count(1); // count no.of 1's for top level items which are 0 initially but when data() called there I am inserting 2 child's (1's), so when next time rowCount() is called it returns 2, but first time it would be 0. I am doing this because rowCount() calls for all the top level items initially that time I am simply passing 0, but again when data() is called rowCount() will be called one more time (for visible items only) that time I am inserting 2 items for top level item in data() function so rowCount() will return 2 this time.
  9. }
  10. return 0;
  11. }
  12.  
  13. QVariant Model::data(const QModelIndex &index, int role ) const
  14. {
  15. if(index.isValid()){
  16. QVector<int> row;
  17. row << 1 << 1;
  18. childs[index.row()] = row; // for all top level items inserting 2 child's
  19. }
  20.  
  21. if(role == Qt::DisplayRole)
  22. return index.row()+1;
  23.  
  24. return QVariant();
  25. }
To copy to clipboard, switch view to plain text mode 


But my problem is I am not able to see plus sign for all the top level items in tree view (except 1st top level item).
But if i double click on visible top level item it is expanding with 2 child's (as expected) but there is no + sign which indicates that there are child's.
I implemented hasChildren() also and checked it did not work.

Any idea/suggestions for this ?? (I tested this with Qt4.8.6 & 5.4.0 on windows8)
Thanks in advance.

My complete model is here, if any one want to test:

Qt Code:
  1. Model::Model(QObject *parent): QAbstractItemModel(parent)
  2. {
  3. childs.resize(5000);
  4. }
  5.  
  6. Model::~Model()
  7. {
  8. }
  9.  
  10. bool Model::hasChildren(const QModelIndex &parent) const
  11. {
  12. if(!parent.isValid())
  13. return true;
  14. if(!parent.parent().isValid()){
  15. int val = childs.at(parent.row()).count(1);
  16. return ( (0 != val) ? true : false);
  17. }
  18. return false;
  19. }
  20.  
  21. QVariant Model::data(const QModelIndex &index, int role ) const
  22. {
  23. if(index.isValid()){
  24. QVector<int> row;
  25. row << 1 << 1;
  26. childs[index.row()] = row;
  27. }
  28.  
  29. if(role == Qt::DisplayRole)
  30. return index.row()+1;
  31.  
  32. return QVariant();
  33. }
  34.  
  35. int Model::columnCount(const QModelIndex &parent) const
  36. {
  37. return 1; //m_head.size();
  38. }
  39.  
  40. int Model::rowCount(const QModelIndex &parent) const
  41. {
  42. if(!parent.isValid()){
  43. return 5000;
  44. }
  45. else if(!parent.parent().isValid()) //limited to 2 levels
  46. {
  47. return childs.at(parent.row()).count(1);
  48. }
  49. return 0;
  50. }
  51.  
  52. QModelIndex Model::index(int row, int column, const QModelIndex &parent) const
  53. {
  54. if(!parent.isValid())
  55. {
  56. return createIndex(row, column, -1); // top level item has invalid parent
  57. }
  58. // we only need id of the parent since it identifies the item
  59. return createIndex(row, column, parent.row());
  60. }
  61.  
  62. QModelIndex Model::parent(const QModelIndex &ind) const
  63. {
  64. if(!ind.isValid()) return QModelIndex();
  65. int id = ind.internalId();
  66. if( id == -1) return QModelIndex();
  67. return index(id, 0); // return top-level item
  68. }
To copy to clipboard, switch view to plain text mode