From ed9665597d1f8c67012e14877faa78e536449ed4 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 12 Oct 2021 11:25:09 +0200 Subject: [PATCH] Eliminate use of goto in QGraphicsWidget::resize() Get rid of the gotos by packaging the wrap-up code in a QScopeGuard. Thanks to Volker Hilsheimer for suggesting that solution. Change-Id: I71bebf59263ce05f111d1fcfcec93f4635a35428 Reviewed-by: Volker Hilsheimer --- src/widgets/graphicsview/qgraphicswidget.cpp | 37 +++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/widgets/graphicsview/qgraphicswidget.cpp b/src/widgets/graphicsview/qgraphicswidget.cpp index a4b1b8ea6c..2745dff351 100644 --- a/src/widgets/graphicsview/qgraphicswidget.cpp +++ b/src/widgets/graphicsview/qgraphicswidget.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtWidgets module of the Qt Toolkit. @@ -55,6 +55,7 @@ #include #endif #include +#include #include #include #include @@ -352,6 +353,19 @@ void QGraphicsWidget::resize(const QSizeF &size) void QGraphicsWidget::setGeometry(const QRectF &rect) { QGraphicsWidgetPrivate *wd = QGraphicsWidget::d_func(); + // Package relayout of children in a scope guard so we can just return early + // when this widget's geometry is sorted out. + const auto relayoutChildren = qScopeGuard([this, wd]() { + if (QGraphicsLayout::instantInvalidatePropagation()) { + if (QGraphicsLayout *lay = wd->layout) { + if (!lay->isActivated()) { + QEvent layoutRequest(QEvent::LayoutRequest); + QCoreApplication::sendEvent(this, &layoutRequest); + } + } + } + }); + QGraphicsLayoutItemPrivate *d = QGraphicsLayoutItem::d_ptr.data(); QRectF newGeom; QPointF oldPos = d->geom.topLeft(); @@ -361,9 +375,8 @@ void QGraphicsWidget::setGeometry(const QRectF &rect) newGeom.setSize(rect.size().expandedTo(effectiveSizeHint(Qt::MinimumSize)) .boundedTo(effectiveSizeHint(Qt::MaximumSize))); - if (newGeom == d->geom) { - goto relayoutChildrenAndReturn; - } + if (newGeom == d->geom) + return; // setPos triggers ItemPositionChange, which can adjust position wd->inSetGeometry = 1; @@ -371,9 +384,8 @@ void QGraphicsWidget::setGeometry(const QRectF &rect) wd->inSetGeometry = 0; newGeom.moveTopLeft(pos()); - if (newGeom == d->geom) { - goto relayoutChildrenAndReturn; - } + if (newGeom == d->geom) + return; // Update and prepare to change the geometry (remove from index) if the size has changed. if (wd->scene) { @@ -396,7 +408,7 @@ void QGraphicsWidget::setGeometry(const QRectF &rect) //set the new pos d->geom.moveTopLeft(pos()); emit geometryChanged(); - goto relayoutChildrenAndReturn; + return; } } QSizeF oldSize = size(); @@ -423,15 +435,6 @@ void QGraphicsWidget::setGeometry(const QRectF &rect) } emit geometryChanged(); -relayoutChildrenAndReturn: - if (QGraphicsLayout::instantInvalidatePropagation()) { - if (QGraphicsLayout *lay = wd->layout) { - if (!lay->isActivated()) { - QEvent layoutRequest(QEvent::LayoutRequest); - QCoreApplication::sendEvent(this, &layoutRequest); - } - } - } } /*!