Skip to content

Commit 0d7bb07

Browse files
authored
Suppress the crash when opening the context menu on a text field in the diagram editor (#2088)
* Suppress the crash when opening the context menu on a text field in the chart editor * Fix Vera++ warnings * Improve Code ----------- Problem Opening the context menu on a text field (e.g., label item) inside the diagram editor causes a crash in builds using Qt 5.15.2. The crash originates from a known bug in this Qt version, which is not present in newer releases (≥5.15.3). Unfortunately, the current Qt SDK does not provide a newer version, making a direct fix impossible at this time. Solution This PR suppresses the crash by preventing the context menu from being triggered on the graphical text item. Instead, users can still access and modify text properties via the property editor panel (located on the top-right side of the window). This approach eliminates the crash while preserving the ability to change properties through an alternative UI element. Limitations The context menu on the text field itself will no longer appear. Full functionality will only be restored after upgrading to Qt 5.15.3+ or refactoring the affected components—both of which are considered non-trivial and are not planned for the near future. Impact No more crashes when right‑clicking on text fields. Users must use the property editor panel to modify text properties. This is a temporary workaround to improve stability until a more comprehensive solution becomes feasible. How to test Open a diagram with a text field (e.g., a label block). Right‑click on the text field – the application should not crash. Verify that text properties can still be edited via the property editor panel (top‑right).
1 parent ad7dc6a commit 0d7bb07

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

qrgui/editor/labels/label.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ using namespace qReal::gui::editor;
2525

2626
Label::Label(models::GraphicalModelAssistApi &graphicalAssistApi
2727
, models::LogicalModelAssistApi &logicalAssistApi
28+
// NOLINTNEXTLINE(modernize-pass-by-value)
2829
, const Id &elementId
2930
, const QSharedPointer<LabelProperties> &properties)
3031
: mIsStretched(false)
@@ -44,9 +45,7 @@ Label::Label(models::GraphicalModelAssistApi &graphicalAssistApi
4445
setAcceptDrops(true);
4546
}
4647

47-
Label::~Label()
48-
{
49-
}
48+
Label::~Label() = default;
5049

5150
const LabelProperties &Label::info() const
5251
{
@@ -188,7 +187,7 @@ QString Label::location() const
188187
void Label::updateData(bool withUndoRedo)
189188
{
190189
const QString value = toPlainText();
191-
Element * const parent = dynamic_cast<Element *>(parentItem());
190+
auto * const parent = dynamic_cast<Element *>(parentItem());
192191
if (!mProperties->nameForRoleProperty().isEmpty()) {
193192
if (mEnumValues.isEmpty()) {
194193
parent->setLogicalProperty(mProperties->nameForRoleProperty()
@@ -197,7 +196,8 @@ void Label::updateData(bool withUndoRedo)
197196
, withUndoRedo
198197
);
199198
} else {
200-
const QString repoValue = mEnumValues.values().contains(value)
199+
const auto &values = mEnumValues.values();
200+
const QString repoValue = values.contains(value)
201201
? mEnumValues.key(value)
202202
: (withUndoRedo ? enumText(value) : value);
203203
parent->setLogicalProperty(mProperties->nameForRoleProperty()
@@ -234,7 +234,8 @@ void Label::updateData(bool withUndoRedo)
234234

235235
parent->setLogicalProperty(mProperties->binding(), mTextBeforeTextInteraction, value, withUndoRedo);
236236
} else {
237-
const QString repoValue = mEnumValues.values().contains(value)
237+
const auto &values = mEnumValues.values();
238+
const QString repoValue = values.contains(value)
238239
? mEnumValues.key(value)
239240
: (withUndoRedo ? enumText(value) : value);
240241
parent->setLogicalProperty(mProperties->binding(), mTextBeforeTextInteraction, repoValue, withUndoRedo);
@@ -264,7 +265,8 @@ void Label::mousePressEvent(QGraphicsSceneMouseEvent *event)
264265
{
265266
if (dynamic_cast<EdgeElement *>(parentItem())) {
266267
// Passing event to edge because users usially want to edit its property when clicking on it.
267-
QGraphicsItem::mousePressEvent(event);
268+
// NOLINTNEXTLINE(bugprone-parent-virtual-call)
269+
QGraphicsItem::mousePressEvent(event); //clazy:exclude=skipped-base-method
268270
return;
269271
}
270272

@@ -350,6 +352,15 @@ bool Label::isReadOnly() const
350352
return mProperties->isReadOnly();
351353
}
352354

355+
// https://qt-project.atlassian.net/browse/QTBUG-88309#icft=QTBUG-88309
356+
// TODO: Just remove this Label::contextMenuEvent override when upgrading to Qt >= 5.15.3
357+
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 3)
358+
void Label::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
359+
{
360+
event->accept();
361+
}
362+
#endif
363+
353364
void Label::focusOutEvent(QFocusEvent *event)
354365
{
355366
if (event->reason() != Qt::PopupFocusReason) {
@@ -471,7 +482,7 @@ void Label::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWi
471482

472483
// Default dashed frame is drawn arround the whole bounding rect (arround prefix and suffix too). Disabling it.
473484
const_cast<QStyleOptionGraphicsItem *>(option)->state &= ~QStyle::State_Selected & ~QStyle::State_HasFocus;
474-
485+
475486
QGraphicsTextItem::paint(painter, option, widget);
476487
}
477488

qrgui/editor/labels/label.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,13 @@ class Label : public QGraphicsTextItem, public LabelInterface
7979
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
8080
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
8181
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
82-
82+
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 3)
83+
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
84+
#endif
8385
void focusOutEvent(QFocusEvent *event) override;
8486
void keyPressEvent(QKeyEvent *event) override;
8587

86-
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
88+
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
8789
void drawText(QPainter *painter, const QRectF &rect, const QString &text);
8890
QRectF prefixRect() const;
8991
QRectF suffixRect() const;

0 commit comments

Comments
 (0)