From 03e76337cae67bb718a4998c9e31badd295a9a61 Mon Sep 17 00:00:00 2001 From: Daniel Teske Date: Wed, 2 Oct 2013 16:17:25 +0200 Subject: [PATCH] AndroidExtraLibraryListModel: Fix crash on removing libs Steps to reproduce the crash, start with the list a b a c, select the second a and the c. Click on remove. The removeEntries will find that it can remove two consecutive entries in one beginRemoveRows/endRemoveRows, but will wrongly remove them starting at the first a. The fix is too simply order the modelindexes in descendeding order such that removing entries does not modify the rows. Change-Id: I4be349f4bab8137075da0d8dfcef24f10dc25f92 Reviewed-by: Eskil Abrahamsen Blomfeldt --- .../android/androidextralibrarylistmodel.cpp | 26 ++++++++++--------- .../android/androidextralibrarylistmodel.h | 2 +- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/plugins/android/androidextralibrarylistmodel.cpp b/src/plugins/android/androidextralibrarylistmodel.cpp index c3a02caf247..491c3d50657 100644 --- a/src/plugins/android/androidextralibrarylistmodel.cpp +++ b/src/plugins/android/androidextralibrarylistmodel.cpp @@ -102,27 +102,29 @@ void AndroidExtraLibraryListModel::addEntries(const QStringList &list) endInsertRows(); } -void AndroidExtraLibraryListModel::removeEntries(const QModelIndexList &list) +bool greaterModelIndexByRow(const QModelIndex &a, const QModelIndex &b) +{ + return a.row() > b.row(); +} + +void AndroidExtraLibraryListModel::removeEntries(QModelIndexList list) { if (list.isEmpty() || m_project->rootQt4ProjectNode()->projectType() != Qt4ProjectManager::ApplicationTemplate) return; - QStringList oldList = m_entries; + std::sort(list.begin(), list.end(), greaterModelIndexByRow); + int i = 0; while (i < list.size()) { - int firstRow = list.at(i++).row(); - int lastRow = firstRow; - while (i < list.size() && list.at(i).row() - lastRow <= 1 && list.at(i).row() > firstRow) - lastRow = list.at(i++).row(); + int lastRow = list.at(i++).row(); + int firstRow = lastRow; + while (i < list.size() && firstRow - list.at(i).row() <= 1) + firstRow = list.at(i++).row(); - int first = m_entries.indexOf(oldList.at(firstRow)); + beginRemoveRows(QModelIndex(), firstRow, lastRow); int count = lastRow - firstRow + 1; - Q_ASSERT(count > 0); - Q_ASSERT(oldList.at(lastRow) == m_entries.at(first + count - 1)); - - beginRemoveRows(QModelIndex(), first, first + count - 1); while (count-- > 0) - m_entries.removeAt(first); + m_entries.removeAt(firstRow); endRemoveRows(); } diff --git a/src/plugins/android/androidextralibrarylistmodel.h b/src/plugins/android/androidextralibrarylistmodel.h index 54a486bd269..a4f5f203a34 100644 --- a/src/plugins/android/androidextralibrarylistmodel.h +++ b/src/plugins/android/androidextralibrarylistmodel.h @@ -51,7 +51,7 @@ public: int columnCount(const QModelIndex &parent) const; QVariant data(const QModelIndex &index, int role) const; - void removeEntries(const QModelIndexList &list); + void removeEntries(QModelIndexList list); void addEntries(const QStringList &list); private slots: