Keep the text resize handles working after a scene change

A scene drops the event filters of an item that leaves it. The handles
installed theirs once, in the constructor, and only if the text was in
a scene: after the text was removed and added back (undoing a delete
does that), or when the handles were made before the text was in a
scene, they no longer reacted, and a drag moved the text instead.
TextResizeHandles now installs the filters again whenever it enters a
scene.

Tests: dragAfterLeavingTheScene, dragWhenCreatedOutsideAScene (both
fail before).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Beat Hangartner
2026-10-06 09:46:17 +02:00
parent 010e44ab8a
commit 5cad0c6159
3 changed files with 65 additions and 3 deletions
+32
View File
@@ -215,6 +215,38 @@ private slots:
}
}
// The handles still work after the text left its scene and came back,
// as undoing a delete does: Qt drops scene event filters on removal.
void dragAfterLeavingTheScene()
{
QGraphicsScene scene;
auto *text = new WidthText(QStringLiteral("Motor protection switch Q12"));
scene.addItem(text);
auto *handles = new TextResizeHandles(text);
scene.removeItem(text);
scene.addItem(text);
const QPointF start_pos = text->pos();
drag(scene, *handles, *text, TextResize::BottomRight, QPointF(-40, 0));
QVERIFY(text->textWidth() > 0);
QCOMPARE(text->pos(), start_pos);
delete text;
}
// The handles also work when they are created before the text is in a
// scene.
void dragWhenCreatedOutsideAScene()
{
QGraphicsScene scene;
auto *text = new WidthText(QStringLiteral("Motor protection switch Q12"));
auto *handles = new TextResizeHandles(text);
scene.addItem(text);
drag(scene, *handles, *text, TextResize::BottomRight, QPointF(-40, 0));
QVERIFY(text->textWidth() > 0);
}
// A text is never made narrower than its longest word.
void dragStopsAtTheLongestWord()
{