Merge pull request #1540 from hairykiwi/fix/elements-panel-crashes

Guard elements-panel auto-select against a detached project item
This commit is contained in:
ispyisail
2026-10-19 16:00:33 +13:00
committed by GitHub
3 changed files with 54 additions and 3 deletions
+5 -3
View File
@@ -156,9 +156,11 @@ QTreeWidgetItem *ElementsPanel::addProject(QETProject *project,
if (first_add){
qtwi_project -> setExpanded(true);
// on adding an project select first diagram
setCurrentItem(qtwi_project -> child(0));
qtwi_project -> child(0)->setSelected(true);
// on adding a project select first diagram, once the project item is attached
if (qtwi_project -> treeWidget()) {
setCurrentItem(qtwi_project -> child(0));
qtwi_project -> child(0)->setSelected(true);
}
}
else {
// 2. Check whether we can restore the previous selection
+5
View File
@@ -293,6 +293,11 @@ qet_add_app_test(tst_folioimageload tst_folioimageload.cpp)
# selection has nothing to edit for a moment (#1439).
qet_add_app_test(tst_selectionpanelheight tst_selectionpanelheight.cpp)
# Opening a project before the elements panel's first reload() left its tree
# item detached, and selecting the item's first child crashed in QTreeModel
# (pre-existing since a82f6de23).
qet_add_app_test(tst_elementspanelprojectopen tst_elementspanelprojectopen.cpp)
# SVG export with smaller pictures (--svg-small-pictures, the export dialog's
# option): a picture drawn at a tenth of its size is stored at four times
# that, a small one whole; without the option every picture is whole.
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include "qetapptest.h"
#include "elementspanel.h"
#include "projectview.h"
#include "qetproject.h"
// projectWasOpened() adds the project to the elements panel. Before the panel's
// first reload() the project's tree item is left detached (treeWidget() ==
// nullptr), and selecting its first child crashed in QTreeModel
// (pre-existing since a82f6de23).
class tst_elementspanelprojectopen : public QObject
{
Q_OBJECT
private slots:
void addProjectBeforeReloadDoesNotCrash()
{
// A real one-folio project from the running app; diagrams()
// builds the lazy folio, so the project item will have a child.
QETDiagramEditor *editor = QetAppTest::editor();
QVERIFY(editor);
QVERIFY(editor->newProject());
ProjectView *pv = editor->currentProjectView();
QVERIFY(pv);
QETProject *project = pv->project();
QVERIFY(project);
QVERIFY(!project->diagrams().isEmpty());
// Fresh panel: reload() has not run, so the project item is inserted detached.
ElementsPanel panel;
// Without the guard this selects a detached item; with it, nothing is selected.
panel.projectWasOpened(project);
QTreeWidgetItem *current = panel.currentItem();
QVERIFY2(current == nullptr || current->treeWidget() == &panel,
"the panel selected a detached item it does not own");
}
};
QET_APP_TEST_MAIN(tst_elementspanelprojectopen)
#include "tst_elementspanelprojectopen.moc"