feat(StatusQ/TestHelpers): Add method to test model removing items and emiting layoutChanged

This commit is contained in:
Michał Cieślak 2024-04-09 14:12:15 +02:00 committed by Michał
parent 42a60642e8
commit 598a389c3e
2 changed files with 36 additions and 0 deletions

View File

@ -85,6 +85,9 @@ void TestModel::remove(int index)
void TestModel::invert()
{
if (m_data.size() < 2)
return;
emit layoutAboutToBeChanged();
for (auto& entry : m_data)
@ -99,6 +102,32 @@ void TestModel::invert()
emit layoutChanged();
}
void TestModel::removeEverySecond()
{
if (m_data.empty())
return;
emit layoutAboutToBeChanged();
for (auto& entry : m_data) {
QVariantList& data = entry.second;
for (auto i = 0; i < data.size(); i++)
data.removeAt(i);
}
const auto persistentIndexes = persistentIndexList();
for (const QModelIndex& index : persistentIndexes) {
if (index.row() % 2 == 0)
changePersistentIndex(index, {});
else
changePersistentIndex(index, createIndex(index.row() / 2, 0));
}
emit layoutChanged();
}
void TestModel::initRoles()
{
m_roles.reserve(m_data.size());

View File

@ -19,6 +19,13 @@ public:
// inverts order of items, emits layoutAboutToBeChanged / layoutChanged
void invert();
// removes every second item from the model but doesn't emit
// rowsAboutToBeRemoved/rowsRemoved. The update is notified via
// layoutAboutToBeChanged/layoutChanged. It's useful for testing proxy
// models against that scenario, which may occur in some circumstances, e.g.
// during SFPM initialization where initial filtering is notified this way.
void removeEverySecond();
private:
void initRoles();