SRD

treeviews

SRD
Mar 3rd, 2019
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.97 KB | None | 0 0
  1. // I'm trying to convert the Simple Tree Model Example to be called in
  2. // the following way instead of the file that's passed in and parsed.
  3.  
  4. // Here I create the root item and pass it to the model.
  5.     QList<QVariant> root_data;                                                
  6.     root_data << "Column 1" << "Column 2";
  7.  
  8.     MyTreeItem *parent_item = new MyTreeItem(root_data);            
  9.  
  10.     tree_model = new MyTreeModel(parent_item);
  11.  
  12. // Then I create a child and add it.
  13.     QList<QVariant> mp_data;
  14.     mp_data << "ABC" << "DEF";
  15.  
  16.     MyTreeItem *mp_item = new MyTreeItem(mp_data);
  17.    
  18.     parent_item->appendChild(mp_item);
  19.  
  20. // The problem is, it's crashing in MyTreeModel::rowCount().
  21. // Here's the MyTreeModel.cpp
  22.  
  23. MyTreeModel::MyTreeModel(MyTreeItem *item, QObject *parent) :
  24.     QAbstractItemModel(parent)
  25.  
  26. {
  27.     root_item = item;
  28. }
  29.  
  30. MyTreeModel::~MyTreeModel()
  31. {
  32.     delete root_item;
  33. }
  34.  
  35. QModelIndex MyTreeModel::index(int row, int column,
  36.     const QModelIndex &parent) const
  37. {
  38.     if (!hasIndex(row, column, parent))
  39.         return QModelIndex();
  40.  
  41.     MyTreeItem *parent_item;
  42.  
  43.     if (!parent.isValid())
  44.         parent_item = root_item;
  45.     else
  46.         parent_item = static_cast<MyTreeItem *>(parent.internalPointer());
  47.  
  48.     MyTreeItem *child_item = parent_item->child(row);
  49.  
  50.     if (child_item)
  51.         return createIndex(row, column, child_item);
  52.     else
  53.         return QModelIndex();
  54. }
  55.  
  56. QModelIndex MyTreeModel::parent(const QModelIndex &index) const
  57. {
  58.     if (!index.isValid())
  59.         return QModelIndex();
  60.  
  61.     MyTreeItem *child_item = static_cast<MyTreeItem *>
  62.         (index.internalPointer());
  63.     MyTreeItem *parent_item = child_item->parent();
  64.  
  65.     if (parent_item == root_item)
  66.         return QModelIndex();
  67.  
  68.     return createIndex(parent_item->row(), 0, parent_item);
  69. }
  70.  
  71. int MyTreeModel::rowCount(const QModelIndex &parent) const
  72. {
  73.     MyTreeItem *parent_item;
  74.  
  75.     if (parent.column() > 0)
  76.         return 0;
  77.  
  78.     if (!parent.isValid())
  79.         parent_item = root_item;
  80.     else
  81.         // it's crashing here ...
  82.         parent_item = static_cast<MyTreeItem *>(parent.internalPointer());
  83.                                                                                
  84.     return parent_item->childCount();                                          
  85. }
  86.                                                                                
  87. int MyTreeModel::columnCount(const QModelIndex &parent) const            
  88. {                                                                              
  89.     if (parent.isValid())
  90.         return static_cast<MTreeItem *>(parent.internalPointer())->columnCount();
  91.     else                                                                      
  92.         return root_item->columnCount();                                      
  93. }                                                                              
  94.                                                                                
  95. Qt::ItemFlags MyTreeModel::flags(const QModelIndex &index) const
  96. {                                                                              
  97.     if (!index.isValid())
  98.         return 0;
  99.                                                                                
  100.     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
  101. }
  102.                                                                    
  103. QVariant MyTreeModel::data(const QModelIndex &index, int role) const
  104. {                                                                              
  105.     if (!index.isValid())
  106.         return QVariant();
  107.                                                                                
  108.     if (role != Qt::DisplayRole)
  109.         return QVariant();
  110.                                                                                
  111.     MyTreeItem *item = static_cast<MyTreeItem *>(index.internalPointer());
  112.                                                                                
  113.     return item->data(index.column());
  114. }                                                                              
  115.  
  116. QVariant MyTreeModel::headerData(int section, Qt::Orientation orientation,
  117.     int role) const
  118. {
  119.     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)              
  120.         return root_item->data(section);                                      
  121.     return QVariant();                                                        
  122. }                
  123.  
  124. // MyTreeItem.cpp
  125.  
  126. MyTreeItem::MyTreeItem(const QList<QVariant> &data, MyTreeItem *parent)
  127. {
  128.     parent_item = parent;                                                      
  129.     item_data = data;
  130. }  
  131.        
  132. MyTreeItem::~MyTreeItem()
  133. {  
  134.     qDeleteAll(child_items);
  135. }
  136.                                                                                
  137. void MyTreeItem::appendChild(MyTreeItem *item)
  138. {  
  139.     child_items.append(item);
  140. }
  141.    
  142. MyTreeItem* MyTreeItem::child(int row)
  143. {  
  144.     return child_items.value(row);
  145. }  
  146.  
  147. int MyTreeItem::childCount() const
  148. {                                                                              
  149.     return child_items.count();
  150. }
  151.    
  152. int MyTreeItem::row() const                                              
  153. {
  154.     if (parent_item)
  155.         return parent_item->child_items.indexOf(const_cast<MyTreeItem*>(this));
  156.    
  157.     return 0;
  158. }
  159.    
  160. int MyTreeItem::columnCount() const                                      
  161. {  
  162.     return item_data.count();
  163. }                                                                              
  164.    
  165. QVariant MyTreeItem::data(int column) const                              
  166. {  
  167.     return item_data.value(column);
  168. }  
  169.    
  170. MyTreeItem* MyTreeItem::parent()
  171. {  
  172.     return parent_item;
  173. }                                                                              
  174.    
  175. bool MyTreeItem::isChecked() const
  176. {  
  177.     return checked;
  178. }  
  179.  
  180. void MyTreeItem::setChecked(bool set)
  181. {
  182.     checked = set;;                                                            
  183. }
Advertisement
Add Comment
Please, Sign In to add comment