Skip to content

Commit fa27393

Browse files
gui: fix more entry tree crashes
1 parent 17a4392 commit fa27393

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

src/gui/EntryTree.cpp

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ void EntryTreeModel::clear() {
265265

266266
void EntryTreeModel::addEntry(const QString& path, bool incremental) {
267267
QStringList components = path.split('/', Qt::SkipEmptyParts);
268-
auto* current = this->root_.get();
268+
auto* current = this->root();
269269
if (!current) {
270270
return;
271271
}
@@ -276,7 +276,15 @@ void EntryTreeModel::addEntry(const QString& path, bool incremental) {
276276
const auto row = static_cast<int>(current->children().size());
277277
if (incremental) {
278278
emit this->layoutAboutToBeChanged();
279-
this->beginInsertRows(this->getIndexAtNode(current), row, row);
279+
280+
QModelIndex parentIndex;
281+
if (current == this->root()) {
282+
parentIndex = this->createIndex(0, 0, this->root());
283+
} else {
284+
parentIndex = this->getIndexAtNode(current);
285+
}
286+
287+
this->beginInsertRows(parentIndex, row, row);
280288
}
281289
current->children().push_back(std::make_unique<EntryTreeNode>(current, components[i], isDir));
282290
if (incremental) {
@@ -288,7 +296,10 @@ void EntryTreeModel::addEntry(const QString& path, bool incremental) {
288296
current = child;
289297
}
290298
if (incremental && current) {
291-
current->parent()->sort(Qt::AscendingOrder);
299+
if (auto* parent = current->parent()) {
300+
parent->sort(Qt::AscendingOrder);
301+
emit this->dataChanged(this->getIndexAtNode(parent), this->getIndexAtNode(parent));
302+
}
292303
}
293304
}
294305

@@ -306,7 +317,15 @@ void EntryTreeModel::removeEntry(const QString& path) {
306317
return child.get() == node;
307318
}); it != parent->children().end()) {
308319
const auto row = static_cast<int>(std::distance(parent->children().begin(), it));
309-
this->beginRemoveRows(this->getIndexAtNode(parent), row, row);
320+
321+
QModelIndex parentIndex;
322+
if (parent == this->root()) {
323+
parentIndex = this->createIndex(0, 0, this->root());
324+
} else {
325+
parentIndex = this->getIndexAtNode(parent);
326+
}
327+
328+
this->beginRemoveRows(parentIndex, row, row);
310329
parent->children().erase(it);
311330
this->endRemoveRows();
312331
}
@@ -320,7 +339,15 @@ void EntryTreeModel::removeEntry(const QString& path) {
320339
return child.get() == parent;
321340
}); it != grandparent->children().end()) {
322341
const auto row = static_cast<int>(std::distance(grandparent->children().begin(), it));
323-
this->beginRemoveRows(this->getIndexAtNode(grandparent), row, row);
342+
343+
QModelIndex grandparentIndex;
344+
if (grandparent == this->root()) {
345+
grandparentIndex = this->createIndex(0, 0, this->root());
346+
} else {
347+
grandparentIndex = this->getIndexAtNode(grandparent);
348+
}
349+
350+
this->beginRemoveRows(grandparentIndex, row, row);
324351
grandparent->children().erase(it);
325352
this->endRemoveRows();
326353
}

0 commit comments

Comments
 (0)