custommodel.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "custommodel.h"
  2. CustomModel::CustomModel(QObject* parent) : QAbstractItemModel(parent)
  3. {
  4. // 初始化数据
  5. ListItem item1;
  6. item1.text = "Item 1";
  7. ListItem item2;
  8. item2.text = "Item 2";
  9. ListItem child1;
  10. child1.text = "Child 1 of Item 2";
  11. item2.children.append(child1);
  12. item1.children.append(child1);
  13. topLevelItems.append(item1);
  14. topLevelItems.append(item2);
  15. }
  16. CustomModel::~CustomModel()
  17. {
  18. // 清理数据
  19. topLevelItems.clear();
  20. }
  21. QModelIndex CustomModel::index(int row, int column, const QModelIndex& parent) const
  22. {
  23. if (!hasIndex(row, column, parent))
  24. return QModelIndex();
  25. if (!parent.isValid())
  26. {
  27. // 一级列表项
  28. if (row < topLevelItems.size())
  29. return createIndex(row, column, &topLevelItems[row]);
  30. }
  31. else
  32. {
  33. // 二级列表项
  34. ListItem* parentItem = static_cast<ListItem*>(parent.internalPointer());
  35. if (row < parentItem->children.size())
  36. return createIndex(row, column, &parentItem->children[row]);
  37. }
  38. return QModelIndex();
  39. }
  40. QModelIndex CustomModel::parent(const QModelIndex& child) const
  41. {
  42. if (!child.isValid())
  43. return QModelIndex();
  44. ListItem* childItem = static_cast<ListItem*>(child.internalPointer());
  45. if (!childItem)
  46. return QModelIndex();
  47. ListItem* parentItem = nullptr;
  48. if (childItem != &topLevelItems[0])
  49. {
  50. for (const ListItem& item : topLevelItems)
  51. {
  52. if (item.children.contains(*childItem))
  53. {
  54. parentItem = const_cast<ListItem*>(&item);
  55. break;
  56. }
  57. }
  58. }
  59. if (parentItem)
  60. {
  61. int row = topLevelItems.indexOf(*parentItem);
  62. return createIndex(row, 0, parentItem);
  63. }
  64. return QModelIndex();
  65. }
  66. int CustomModel::rowCount(const QModelIndex& parent) const
  67. {
  68. if (parent.column() > 0)
  69. return 0;
  70. if (!parent.isValid())
  71. {
  72. // 一级列表的行数
  73. return topLevelItems.size();
  74. }
  75. else
  76. {
  77. // 二级列表的行数
  78. ListItem* parentItem = static_cast<ListItem*>(parent.internalPointer());
  79. if (parentItem)
  80. return parentItem->children.size();
  81. }
  82. return 0;
  83. }
  84. int CustomModel::columnCount(const QModelIndex& parent) const
  85. {
  86. Q_UNUSED(parent);
  87. // 列数为1
  88. return 1;
  89. }
  90. QVariant CustomModel::data(const QModelIndex& index, int role) const
  91. {
  92. if (!index.isValid())
  93. return QVariant();
  94. if (role == Qt::DisplayRole)
  95. {
  96. ListItem* item = static_cast<ListItem*>(index.internalPointer());
  97. if (item)
  98. return item->text;
  99. }
  100. return QVariant();
  101. }
  102. bool CustomModel::setData(const QModelIndex& index, const QVariant& value, int role)
  103. {
  104. if (!index.isValid())
  105. return false;
  106. if (role == Qt::EditRole)
  107. {
  108. ListItem* item = static_cast<ListItem*>(index.internalPointer());
  109. if (item)
  110. {
  111. item->text = value.toString();
  112. emit dataChanged(index, index);
  113. return true;
  114. }
  115. }
  116. return false;
  117. }
  118. bool CustomModel::insertTopLevelItem(int row, const ListItem& item)
  119. {
  120. if (row < 0 || row > topLevelItems.size())
  121. return false;
  122. beginInsertRows(QModelIndex(), row, row);
  123. topLevelItems.insert(row, item);
  124. endInsertRows();
  125. return true;
  126. }
  127. bool CustomModel::insertChildItem(const QModelIndex& parent, int row, const ListItem& item)
  128. {
  129. if (!parent.isValid() || row < 0 || row > rowCount(parent))
  130. return false;
  131. ListItem* parentItem = static_cast<ListItem*>(parent.internalPointer());
  132. if (!parentItem)
  133. return false;
  134. beginInsertRows(parent, row, row);
  135. parentItem->children.insert(row, item);
  136. endInsertRows();
  137. return true;
  138. }
  139. bool CustomModel::removeTopLevelItem(int row)
  140. {
  141. if (row < 0 || row >= topLevelItems.size())
  142. return false;
  143. beginRemoveRows(QModelIndex(), row, row);
  144. topLevelItems.removeAt(row);
  145. endRemoveRows();
  146. return true;
  147. }
  148. bool CustomModel::removeChildItem(const QModelIndex& parent, int row)
  149. {
  150. if (!parent.isValid() || row < 0 || row >= rowCount(parent))
  151. return false;
  152. ListItem* parentItem = static_cast<ListItem*>(parent.internalPointer());
  153. if (!parentItem)
  154. return false;
  155. beginRemoveRows(parent, row, row);
  156. parentItem->children.removeAt(row);
  157. endRemoveRows();
  158. return true;
  159. }
  160. ListItem CustomModel::getItem(const QModelIndex& index) const
  161. {
  162. if (index.isValid())
  163. {
  164. ListItem* item = static_cast<ListItem*>(index.internalPointer());
  165. if (item)
  166. return *item;
  167. }
  168. return ListItem();
  169. }
  170. Qt::ItemFlags CustomModel::flags(const QModelIndex& index) const
  171. {
  172. if (!index.isValid())
  173. return Qt::NoItemFlags;
  174. Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
  175. if (index.column() == 0)
  176. flags |= Qt::ItemIsUserCheckable;
  177. return flags;
  178. }