#include "tools.h" int getLevel(QStandardItem *item) { int level = 0; QStandardItem *parent = item->parent(); if(parent) { while (parent) { level++; parent = parent->parent(); } return level; } else{ return -1; } } QStandardItem* findStandardItemAtLevel(QStandardItemModel *model, int targetLevel, const QString &targetText, QStandardItem *parent, int currentLevel) { if (!model) return nullptr; if (currentLevel == targetLevel) { if (parent) { // 在父项的子树中查找 for (int row = 0; row < parent->rowCount(); ++row) { QStandardItem *item = parent->child(row, 0); if (item && item->text() == targetText) { return item; } } } else { // 在根层级查找 for (int row = 0; row < model->rowCount(); ++row) { QStandardItem *item = model->item(row, 0); if (item && item->text() == targetText) { return item; } } } return nullptr; } if (currentLevel > targetLevel) return nullptr; // 递归查找子项 if (parent) { for (int row = 0; row < parent->rowCount(); ++row) { QStandardItem *child = parent->child(row, 0); QStandardItem *result = findStandardItemAtLevel(model, targetLevel, targetText, child, currentLevel + 1); if (result) return result; } } else { for (int row = 0; row < model->rowCount(); ++row) { QStandardItem *item = model->item(row, 0); QStandardItem *result = findStandardItemAtLevel(model, targetLevel, targetText, item, currentLevel + 1); if (result) return result; } } return nullptr; } QList getAllChildren(QStandardItem* parentItem) { QList children; if (!parentItem) return children; for (int row = 0; row < parentItem->rowCount(); ++row) { for (int column = 0; column < parentItem->columnCount(); ++column) { QStandardItem* childItem = parentItem->child(row, column); if (childItem) { children.append(childItem); } } } return children; } QModelIndex findIndex(const QAbstractItemModel* model, const QVariant& target, int role, const QModelIndex& parent) { for (int row = 0; row < model->rowCount(parent); ++row) { QModelIndex idx = model->index(row, 0, parent); // 假设查找第0列 if (model->data(idx, role) == target) { return idx; // 找到匹配项 } // 递归查找子项 if (model->hasChildren(idx)) { QModelIndex childIdx = findIndex(model, target, role, idx); if (childIdx.isValid()) { return childIdx; } } } return QModelIndex(); // 未找到 } QStandardItem* deepCloneItem(const QStandardItem* source) { if (!source) return nullptr; // 使用 clone() 方法创建副本 QStandardItem* clone = source->clone(); // 递归复制所有子项 for (int row = 0; row < source->rowCount(); ++row) { for (int col = 0; col < source->columnCount(); ++col) { if (QStandardItem* child = source->child(row, col)) { clone->setChild(row, col, deepCloneItem(child)); } } } return clone; } QStandardItemModel* deepCloneModel(const QStandardItemModel* source) { if (source == nullptr) return nullptr; // 设置相同的行列数 QStandardItemModel* dest = new QStandardItemModel(source->parent()); dest->setRowCount(source->rowCount()); dest->setColumnCount(source->columnCount()); // 复制所有数据项 for (int row = 0; row < source->rowCount(); ++row) { for (int col = 0; col < source->columnCount(); ++col) { if (QStandardItem* item = source->item(row, col)) { dest->setItem(row, col, deepCloneItem(item)); } } } return dest; }