#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(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(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(&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(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(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(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(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(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(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; }