1、判断是否为根节点
QModelIndex currentIndex = treeView->currentIndex();
QStandardItem* currentItem = model->itemFromIndex(currentIndex);
// 如果是根目录,是否存在父节点来判断是否为根目录
bool isRootItem = (currentItem->parent() == nullptr);
2、存储并获取数据
// 存储数据
QStandardItem* item = new QStandardItem("新增item");
item->setData(id, Qt::UserRole + 1); // 用户自定义数据
item->setData(name, Qt::DisplayRole); // 显示的数据
// 获取id
QVariant idData = graphics_treeModel_->data(currentIndex, Qt::UserRole +1);
int id = idData.toInt();
3、索引转item
QModelIndex currentIndex = treeView->currentIndex();
if (!currentIndex.isValid())
return;
QStandardItem* currentItem = model->itemFromIndex(currentIndex);
4、QStandardItem添加子item
QStandardItem* new_item = new QStandardItem(str);
fathertItem->appendRow(new_item);