2025-04-30 16:29:17 +08:00
|
|
|
#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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-16 19:20:46 +08:00
|
|
|
|
|
|
|
|
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(); // 未找到
|
|
|
|
|
}
|
2025-06-27 19:17:04 +08:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|