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(); }