tst_QGraphicsLinearLayout: fix memleaks in defaultSpacing()

QStyle objects aren't owned by widgets (or QGraphicsWidgets) they are
set on, so need to be deleted manually. The test function didn't do
that, so they style objects were leaked.

To fix, hold them in unique_ptrs. QStyle subclasses, even though being
QObjects, don't have the usual QObject *parent=nullptr constructor
parameter, so passing a QObject parent isn't an alternative, really.

Amends 66b7e7497c3b318dddeb91175d8f85f606c99eec.

Pick-to: 6.9 6.8 6.5 5.15
Change-Id: I6c43264760f7126610fe51f72e74ec341bd2e3d7
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
This commit is contained in:
Marc Mutz 2025-03-27 18:03:35 +01:00
parent 64060cbeae
commit dd9a94a3b3

View File

@ -886,15 +886,15 @@ void tst_QGraphicsLinearLayout::defaultSpacing()
{
QGraphicsScene scene;
QGraphicsView view(&scene);
LayoutStyle *style = new LayoutStyle(QLatin1String("windows"));
const auto style = std::make_unique<LayoutStyle>(QLatin1String("windows"));
style->horizontalSpacing = 5;
style->verticalSpacing = 3;
LayoutStyle *style2 = new LayoutStyle(QLatin1String("windows"));
const auto style2 = std::make_unique<LayoutStyle>(QLatin1String("windows"));
style2->horizontalSpacing = 25;
style2->verticalSpacing = 23;
QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window);
widget->setStyle(style);
widget->setStyle(style.get());
// Horizontal layout
SubQGraphicsLinearLayout *layout = new SubQGraphicsLinearLayout(Qt::Horizontal);
@ -921,13 +921,13 @@ void tst_QGraphicsLinearLayout::defaultSpacing()
QCOMPARE(styleSpacing, qreal(15));
QCOMPARE(styleSpacing, layout->spacing());
QCOMPARE(layout->effectiveSizeHint(Qt::PreferredSize).width(), qreal(115));
widget->setStyle(style2);
widget->setStyle(style2.get());
// If the style itself changes, the layout will pick that up
QCOMPARE(layout->effectiveSizeHint(Qt::PreferredSize).width(), qreal(125));
QCOMPARE(layout->spacing(), qreal(25));
// Vertical layout
widget->setStyle(style);
widget->setStyle(style.get());
layout->setOrientation(Qt::Vertical);
styleSpacing = (qreal)style->pixelMetric(QStyle::PM_LayoutVerticalSpacing);
QCOMPARE(styleSpacing, qreal(3));
@ -940,7 +940,7 @@ void tst_QGraphicsLinearLayout::defaultSpacing()
QCOMPARE(styleSpacing, qreal(13));
QCOMPARE(styleSpacing, layout->spacing());
QCOMPARE(layout->effectiveSizeHint(Qt::PreferredSize).height(), qreal(113));
widget->setStyle(style2);
widget->setStyle(style2.get());
// If the style itself changes, the layout will pick that up
QCOMPARE(layout->effectiveSizeHint(Qt::PreferredSize).height(), qreal(123));
QCOMPARE(layout->spacing(), qreal(23));