Fix memory leak and clean up splitter test

Allocate the QSplitter on the stack so that it and its child widgets are
cleaned up when the test function finishes.

As a drive-by, replace QString usage with QByteArray to avoid unneeded
conversion from and to latin1, and modernize list construction and for loop.

Pick-to: 6.4 6.2
Change-Id: I2e29961edbab1ec88be356fca6bc100f08894e82
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
Volker Hilsheimer 2022-10-11 22:14:40 +02:00
parent 4df800d938
commit 9eca8d62fa

View File

@ -908,55 +908,56 @@ void tst_QSplitter::rubberBandNotInSplitter()
void tst_QSplitter::task187373_addAbstractScrollAreas_data()
{
QTest::addColumn<QString>("className");
QTest::addColumn<QByteArray>("className");
QTest::addColumn<bool>("addInConstructor");
QTest::addColumn<bool>("addOutsideConstructor");
QStringList classNames;
classNames << QLatin1String("QGraphicsView");
classNames << QLatin1String("QMdiArea");
classNames << QLatin1String("QScrollArea");
classNames << QLatin1String("QTextEdit");
classNames << QLatin1String("QTreeView");
QList<QByteArray> classNames{
"QGraphicsView",
"QMdiArea",
"QScrollArea",
"QTextEdit",
"QTreeView"
};
foreach (QString className, classNames) {
QTest::newRow(qPrintable(className + QLatin1String(" 1"))) << className << false << true;
QTest::newRow(qPrintable(className + QLatin1String(" 2"))) << className << true << false;
QTest::newRow(qPrintable(className + QLatin1String(" 3"))) << className << true << true;
for (const auto &className : qAsConst(classNames)) {
QTest::newRow(qPrintable(className + " 1")) << className << false << true;
QTest::newRow(qPrintable(className + " 2")) << className << true << false;
QTest::newRow(qPrintable(className + " 3")) << className << true << true;
}
}
static QAbstractScrollArea *task187373_createScrollArea(
QSplitter *splitter, const QString &className, bool addInConstructor)
QSplitter *splitter, const QByteArray &className, bool addInConstructor)
{
if (className == QLatin1String("QGraphicsView"))
if (className == "QGraphicsView")
return new QGraphicsView(addInConstructor ? splitter : 0);
if (className == QLatin1String("QMdiArea"))
if (className == "QMdiArea")
return new QMdiArea(addInConstructor ? splitter : 0);
if (className == QLatin1String("QScrollArea"))
if (className == "QScrollArea")
return new QScrollArea(addInConstructor ? splitter : 0);
if (className == QLatin1String("QTextEdit"))
if (className == "QTextEdit")
return new QTextEdit(addInConstructor ? splitter : 0);
if (className == QLatin1String("QTreeView"))
if (className == "QTreeView")
return new QTreeView(addInConstructor ? splitter : 0);
return 0;
}
void tst_QSplitter::task187373_addAbstractScrollAreas()
{
QFETCH(QString, className);
QFETCH(QByteArray, className);
QFETCH(bool, addInConstructor);
QFETCH(bool, addOutsideConstructor);
QVERIFY(addInConstructor || addOutsideConstructor);
QSplitter *splitter = new QSplitter;
splitter->show();
QVERIFY(splitter->isVisible());
QSplitter splitter;
splitter.show();
QVERIFY(splitter.isVisible());
QAbstractScrollArea *w = task187373_createScrollArea(splitter, className, addInConstructor);
QAbstractScrollArea *w = task187373_createScrollArea(&splitter, className, addInConstructor);
QVERIFY(w);
if (addOutsideConstructor)
splitter->addWidget(w);
splitter.addWidget(w);
QTRY_VERIFY(w->isVisible());
QVERIFY(!w->isHidden());