Fix bugtracker #345: crash opening a project with an unlinked contact

A contact (slave) not linked to a coil has no element to take its label
from: DynamicElementTextItem::elementUseForInfo() returns null. Since
8985babfe (#978) the composite-text branch of updateLabel() called
element->actualLabel() without checking, so a text built from %{label}
on such a contact crashed QElectroTech while the project was loading
(QETProject::refresh -> Diagram::refreshContents). The project could not
be opened again.

Check the element as the ElementInfo branch above already does; with no
element, %{label} shows empty, as it did before 8985babfe.

Test: tst_resaveunchanged opens a blank project holding one unlinked
contact with a %{label} text. It fails without the fix (the --resave
crashes) and passes with it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
ispyisail
2026-10-02 18:48:48 +13:00
parent 6c8fb720c1
commit c0b8472665
3 changed files with 339 additions and 4 deletions
@@ -1347,8 +1347,13 @@ void DynamicElementTextItem::updateLabel()
}
else if (m_text_from == CompositeText) {
// Use actualLabel() to ensure %{label} reflects the current
// resolved label (e.g. after a folio/page-number change)
dc.addValue(QStringLiteral("label"), element->actualLabel());
// resolved label (e.g. after a folio/page-number change).
// A contact not linked to a coil has no element to read from
// (bugtracker #345): %{label} then shows empty, as it did
// before actualLabel() was used here.
if (element) {
dc.addValue(QStringLiteral("label"), element->actualLabel());
}
setPlainText(autonum::AssignVariables::replaceVariable(m_composite_text, dc));
}
}