123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- #include "custommodel.h"
- CustomModel::CustomModel(QObject* parent) : QAbstractItemModel(parent)
- {
- // 初始化数据
- ListItem item1;
- item1.text = "Item 1";
- ListItem item2;
- item2.text = "Item 2";
- ListItem child1;
- child1.text = "Child 1 of Item 2";
- item2.children.append(child1);
- item1.children.append(child1);
- topLevelItems.append(item1);
- topLevelItems.append(item2);
- }
- CustomModel::~CustomModel()
- {
- // 清理数据
- topLevelItems.clear();
- }
- QModelIndex CustomModel::index(int row, int column, const QModelIndex& parent) const
- {
- if (!hasIndex(row, column, parent))
- return QModelIndex();
- if (!parent.isValid())
- {
- // 一级列表项
- if (row < topLevelItems.size())
- return createIndex(row, column, &topLevelItems[row]);
- }
- else
- {
- // 二级列表项
- ListItem* parentItem = static_cast<ListItem*>(parent.internalPointer());
- if (row < parentItem->children.size())
- return createIndex(row, column, &parentItem->children[row]);
- }
- return QModelIndex();
- }
- QModelIndex CustomModel::parent(const QModelIndex& child) const
- {
- if (!child.isValid())
- return QModelIndex();
- ListItem* childItem = static_cast<ListItem*>(child.internalPointer());
- if (!childItem)
- return QModelIndex();
- ListItem* parentItem = nullptr;
- if (childItem != &topLevelItems[0])
- {
- for (const ListItem& item : topLevelItems)
- {
- if (item.children.contains(*childItem))
- {
- parentItem = const_cast<ListItem*>(&item);
- break;
- }
- }
- }
- if (parentItem)
- {
- int row = topLevelItems.indexOf(*parentItem);
- return createIndex(row, 0, parentItem);
- }
- return QModelIndex();
- }
- int CustomModel::rowCount(const QModelIndex& parent) const
- {
- if (parent.column() > 0)
- return 0;
- if (!parent.isValid())
- {
- // 一级列表的行数
- return topLevelItems.size();
- }
- else
- {
- // 二级列表的行数
- ListItem* parentItem = static_cast<ListItem*>(parent.internalPointer());
- if (parentItem)
- return parentItem->children.size();
- }
- return 0;
- }
- int CustomModel::columnCount(const QModelIndex& parent) const
- {
- Q_UNUSED(parent);
- // 列数为1
- return 1;
- }
- QVariant CustomModel::data(const QModelIndex& index, int role) const
- {
- if (!index.isValid())
- return QVariant();
- if (role == Qt::DisplayRole)
- {
- ListItem* item = static_cast<ListItem*>(index.internalPointer());
- if (item)
- return item->text;
- }
- return QVariant();
- }
- bool CustomModel::setData(const QModelIndex& index, const QVariant& value, int role)
- {
- if (!index.isValid())
- return false;
- if (role == Qt::EditRole)
- {
- ListItem* item = static_cast<ListItem*>(index.internalPointer());
- if (item)
- {
- item->text = value.toString();
- emit dataChanged(index, index);
- return true;
- }
- }
- return false;
- }
- bool CustomModel::insertTopLevelItem(int row, const ListItem& item)
- {
- if (row < 0 || row > topLevelItems.size())
- return false;
- beginInsertRows(QModelIndex(), row, row);
- topLevelItems.insert(row, item);
- endInsertRows();
- return true;
- }
- bool CustomModel::insertChildItem(const QModelIndex& parent, int row, const ListItem& item)
- {
- if (!parent.isValid() || row < 0 || row > rowCount(parent))
- return false;
- ListItem* parentItem = static_cast<ListItem*>(parent.internalPointer());
- if (!parentItem)
- return false;
- beginInsertRows(parent, row, row);
- parentItem->children.insert(row, item);
- endInsertRows();
- return true;
- }
- bool CustomModel::removeTopLevelItem(int row)
- {
- if (row < 0 || row >= topLevelItems.size())
- return false;
- beginRemoveRows(QModelIndex(), row, row);
- topLevelItems.removeAt(row);
- endRemoveRows();
- return true;
- }
- bool CustomModel::removeChildItem(const QModelIndex& parent, int row)
- {
- if (!parent.isValid() || row < 0 || row >= rowCount(parent))
- return false;
- ListItem* parentItem = static_cast<ListItem*>(parent.internalPointer());
- if (!parentItem)
- return false;
- beginRemoveRows(parent, row, row);
- parentItem->children.removeAt(row);
- endRemoveRows();
- return true;
- }
- ListItem CustomModel::getItem(const QModelIndex& index) const
- {
- if (index.isValid())
- {
- ListItem* item = static_cast<ListItem*>(index.internalPointer());
- if (item)
- return *item;
- }
- return ListItem();
- }
- Qt::ItemFlags CustomModel::flags(const QModelIndex& index) const
- {
- if (!index.isValid())
- return Qt::NoItemFlags;
- Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
- if (index.column() == 0)
- flags |= Qt::ItemIsUserCheckable;
- return flags;
- }
|