From f643dd14df43147a3a2d15f1e1d39c27262333c3 Mon Sep 17 00:00:00 2001 From: youdiansaodongxi Date: Wed, 12 Jun 2024 17:20:10 +0800 Subject: [PATCH] =?UTF-8?q?fix(src/libappdata):=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=85=A8=E5=B1=8F=E6=A8=A1=E5=BC=8F=E4=B8=8B=E5=8D=B8=E8=BD=BD?= =?UTF-8?q?=E5=AE=89=E8=A3=85=E5=BA=94=E7=94=A8=E6=97=B6=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E9=94=99=E4=BD=8D=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libappdata/app-group-model.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/libappdata/app-group-model.cpp b/src/libappdata/app-group-model.cpp index 4974175..4dbb0d4 100644 --- a/src/libappdata/app-group-model.cpp +++ b/src/libappdata/app-group-model.cpp @@ -241,25 +241,34 @@ int AppGroupModel::findGroupIndex(const QModelIndex &sourceIndex) const void AppGroupModel::insertApp(int groupIndex, const QModelIndex &sourceIndex) { + int newIndex = sourceIndex.row(); if (groupIndex < 0 || groupIndex >= m_groups.size()) { - beginInsertRows(QModelIndex(), m_groups.size(), m_groups.size()); - m_groups.append(new QVector(1, sourceIndex.row())); + if (newIndex > 0) { + // 查找前一个item的index + newIndex = findGroupIndex(sourceModel()->index(--newIndex, 0)); + if (newIndex < 0) { + newIndex = m_groups.size(); + } else { + ++newIndex; + } + } + + beginInsertRows(QModelIndex(), newIndex, newIndex); + m_groups.insert(newIndex, new QVector(1, sourceIndex.row())); endInsertRows(); return; } - int index = 0; - int newItem = sourceIndex.row(); QVector *subItems = m_groups[groupIndex]; for (; index < subItems->size(); ++index) { - if (newItem < subItems->at(index)) { + if (newIndex < subItems->at(index)) { break; } } beginInsertRows(AppGroupModel::index(groupIndex, 0, QModelIndex()), index, index); - subItems->insert(index, newItem); + subItems->insert(index, newIndex); endInsertRows(); }