class TreeNode {
	friend class GroupModel;
public:
	explicit TreeNode(const QList<QVariant>& data, TreeNode* parent = nullptr)
		: m_nodeData(data), m_parent(parent) {}

	~TreeNode() {
		qDeleteAll(m_children);
	}

	void appendChild(TreeNode* child) {
		m_children.append(child);
	}

	TreeNode* child(int row) {
		return m_children.value(row);
	}

	int childCount() const {
		return m_children.count();
	}

	int columnCount() const {
		return m_nodeData.count();
	}

	QVariant data(int column) const {
		return m_nodeData.value(column);
	}
	QList<QVariant> rawData()
	{
		return m_nodeData;
	}
	int row() const {
		if (m_parent)
			return m_parent->m_children.indexOf(const_cast<TreeNode*>(this));
		return 0;
	}

	TreeNode* parent() {
		return m_parent;
	}
	void clear()
	{
		for (auto obj : m_children)
		{
			delete obj;
		}
		m_children.clear();
	}
	TreeNode* findGroupByID(int id)
	{
		if (m_nodeData.length()>2&&m_nodeData[1].toBool()&&m_nodeData[2].toInt()==id)
		{
			return this;
		}
		for (auto node:m_children)
		{
			TreeNode * ret=node->findGroupByID(id);
			if (ret!=nullptr)
			{
				return ret;
			}
		}
		return nullptr;
	}

private:
	QList<TreeNode*> m_children;
	QList<QVariant> m_nodeData;  //对于分组节点，第一个值是分组名称，第二个值是true，第三个值是分组ID
	TreeNode* m_parent = nullptr;
};
class GroupModel : public QAbstractItemModel {
	Q_OBJECT

public:
	explicit GroupModel(const QStringList& headers, QObject* parent = nullptr)
		: QAbstractItemModel(parent), m_rootNode(new TreeNode(QList<QVariant>())) {
		QList<QVariant> rootData;
		for (const QString& header : headers) {
			rootData << header;
		}
		m_rootNode = new TreeNode(rootData);
	}

	~GroupModel() override {
		delete m_rootNode;
	}

	// 基本模型接口实现
	QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override {
		if (!hasIndex(row, column, parent)) {
			return QModelIndex();
		}

		TreeNode* parentNode = nodeFromIndex(parent);
		TreeNode* childNode = parentNode->child(row);
		return childNode ? createIndex(row, column, childNode) : QModelIndex();
	}

	QModelIndex parent(const QModelIndex& index) const override {
		if (!index.isValid()) {
			return QModelIndex();
		}

		TreeNode* childNode = nodeFromIndex(index);
		TreeNode* parentNode = childNode->parent();

		if (parentNode == m_rootNode) {
			return QModelIndex();
		}

		return createIndex(parentNode->row(), 0, parentNode);
	}

	int rowCount(const QModelIndex& parent = QModelIndex()) const override {
		if (parent.column() > 0) {
			return 0;
		}

		TreeNode* parentNode = nodeFromIndex(parent);
		return parentNode->childCount();
	}

	int columnCount(const QModelIndex& parent = QModelIndex()) const override {
		return m_rootNode->columnCount();
	}

	QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override {
		if (!index.isValid() ) {
			return QVariant();
		}
		TreeNode* node = nodeFromIndex(index);
		if (role == Qt::UserRole)
			return node->rawData();
		else if(role==Qt::DisplayRole)
			return node->data(index.column());
		return QVariant();
	}
	QVariant headerData(int section, Qt::Orientation orientation, int role) const override {
		if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
			return m_rootNode->data(section);
		}
		return QVariant();
	}

	Qt::ItemFlags flags(const QModelIndex& index) const override {
		if (!index.isValid()) {
			return Qt::NoItemFlags;
		}
		return QAbstractItemModel::flags(index);
	}
	TreeNode* getRootNode() { return m_rootNode; }
	//节点操作
	TreeNode* findGroupByID(int id)
	{
		return m_rootNode->findGroupByID(id);
	}
	//添加子分组
	void newGroup(QModelIndex index,int id, int parentID, const QString& groupName)
	{
		TreeNode* parentNode = (parentID == -1) ? m_rootNode : findGroupByID(parentID);
		if (!parentNode) return;

		int row = parentNode->childCount();

		// 关键修改：判断是否是根节点,根节点使用空索引
		QModelIndex parentIndex=parentNode == m_rootNode?QModelIndex(): createIndex(parentNode->row(), 0, parentNode);
		beginInsertRows(parentIndex, row, row);
		QList<QVariant> groupNodeData;
		groupNodeData << groupName << true << id;
		TreeNode* newGroup = new TreeNode(groupNodeData, parentNode);
		parentNode->appendChild(newGroup);
		endInsertRows();
	}
	//重命名
	void newName(const QModelIndex& index, const QString& newName)
	{
		if (!index.isValid())return;
		TreeNode* node = nodeFromIndex(index);
		node->m_nodeData[0] = newName;
		emit dataChanged(index, index);
	}
	//删除节点及其所有子节点
	void del(const QModelIndex& index)
	{
		if (!index.isValid())
			return;

		TreeNode* node = nodeFromIndex(index);
		TreeNode* parentNode = node->parent();

		if (!parentNode)
			return;

		int row = node->row();

		beginRemoveRows(parent(index), row, row);
		delGroupAndArticle(node);
		parentNode->m_children.removeAt(row);
		QSqlQuery query;
		query.prepare("DELETE FROM T_Group WHERE id=:groupid");
		query.bindValue(":groupid", node->m_nodeData[2].toInt());
		if (query.exec())
			delete node;
		endRemoveRows();
	}

	void delGroupAndArticle(TreeNode* node)
	{
		// 先处理子节点
		while (!node->m_children.isEmpty()) {
			TreeNode* childNode = node->m_children.first();

			if (childNode->m_nodeData[1].toBool()) { // 分组节点
				delGroupAndArticle(childNode);
				QSqlQuery query;
				query.prepare("DELETE FROM T_Group WHERE id=:groupid");
				query.bindValue(":groupid", childNode->m_nodeData[2].toInt());
				if (query.exec()) {
					node->m_children.removeOne(childNode);
					delete childNode;
				}
			}
			else { // 网文节点
				QSqlQuery query;
				query.prepare("DELETE FROM T_Article WHERE id=:articleID");
				query.bindValue(":articleID", childNode->m_nodeData[2].toInt());
				if (query.exec()) {
					node->m_children.removeOne(childNode);
					delete childNode;
				}
			}
		}
	}

private:
	TreeNode* nodeFromIndex(const QModelIndex& index) const {
		if (!index.isValid()) {
			return m_rootNode;
		}
		else {
			return static_cast<TreeNode*>(index.internalPointer());
		}
	}

	TreeNode* m_rootNode;
};
对应的数据库表格结构：CREATE TABLE T_Group (
    ID        INTEGER PRIMARY KEY AUTOINCREMENT,
    GroupName TEXT    UNIQUE
                      NOT NULL,
    ParentID  INTEGER NOT NULL
);
请根据上市内容完善优化代码