From 3ab6653d97a5dd01fd5f5cb4e52d5816274e4cbd Mon Sep 17 00:00:00 2001 From: Jan Arve Saether Date: Fri, 6 Feb 2015 14:47:16 +0100 Subject: [PATCH] Fixed crash when adding items with spans This only occurred if the item added was the only item in the last row/column, since then the the internal grid failed to expand (in this case it would always call expand(0, 0), which would not create the extra row/column). [ChangeLog][QtWidgets][QGridLayout] Fixed crash that sometimes happened when adding items with spans that spanned to the bottom/right edge. Task-number: QTBUG-38052 Change-Id: Iba95f6d9d9356b4d1c84c7b93f4af9b4ea0cf714 Reviewed-by: Paul Olav Tvete --- src/widgets/kernel/qgridlayout.cpp | 2 +- .../kernel/qgridlayout/tst_qgridlayout.cpp | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/widgets/kernel/qgridlayout.cpp b/src/widgets/kernel/qgridlayout.cpp index 10d338c40c..3de0eb7960 100644 --- a/src/widgets/kernel/qgridlayout.cpp +++ b/src/widgets/kernel/qgridlayout.cpp @@ -565,7 +565,7 @@ void QGridLayoutPrivate::add(QGridBox *box, int row1, int row2, int col1, int co add(box, row1, col1); return; } - expand(row2 + 1, col2 + 1); + expand(qMax(row1, row2) + 1, qMax(col1, col2) + 1); box->row = row1; box->col = col1; diff --git a/tests/auto/widgets/kernel/qgridlayout/tst_qgridlayout.cpp b/tests/auto/widgets/kernel/qgridlayout/tst_qgridlayout.cpp index 004b3b1731..046d6e032f 100644 --- a/tests/auto/widgets/kernel/qgridlayout/tst_qgridlayout.cpp +++ b/tests/auto/widgets/kernel/qgridlayout/tst_qgridlayout.cpp @@ -96,6 +96,7 @@ private slots: void taskQTBUG_40609_addingWidgetToItsOwnLayout(); void taskQTBUG_40609_addingLayoutToItself(); void replaceWidget(); + void dontCrashWhenExtendsToEnd(); private: QWidget *testWidget; @@ -1728,5 +1729,22 @@ void tst_QGridLayout::replaceWidget() } } +void tst_QGridLayout::dontCrashWhenExtendsToEnd() +{ + QWidget window; + window.resize(320,200); + QWidget parent(&window); + QLabel *lbl0 = new QLabel(QLatin1String("lbl0:")); + QLabel *lbl1 = new QLabel(QLatin1String("lbl1:")); + QPushButton *pb = new QPushButton(QLatin1String("pb1")); + QGridLayout *l = new QGridLayout(&parent); + l->addWidget(lbl0, 0, 0); + l->addWidget(lbl1, 1, 0); + // adding an item in the bottom right corner than spans to the end (!)... + l->addWidget(pb, 1, 1, -1, -1); + // ...should not cause a crash when the items are distributed.... + l->setGeometry(QRect(0, 0, 200, 50)); // DONT CRASH HERE +} + QTEST_MAIN(tst_QGridLayout) #include "tst_qgridlayout.moc"