diff options
author | Thorbjørn Lund Martsum <tmartsum@gmail.com> | 2017-09-01 20:45:27 +0200 |
---|---|---|
committer | Thorbjørn Lund Martsum <tmartsum@gmail.com> | 2021-08-10 12:00:13 +0200 |
commit | 7502598ef5bfa3f1a6d4f435bc2b91914e3acc61 (patch) | |
tree | 66db85b10f0f35a8f9eef654328c0e00d3b4c8c4 /tests/auto/widgets | |
parent | 0e92ec972840e78b6f38d0f554ec1554edf752d1 (diff) |
QListView: fix AdjustToContents (sizeAdjustPolicy)
Unlike an acceptable effect in QTableView + QTreeView
setAdjustPolicy(QAbstractScrollArea::AdjustToContents)
unfortunately didn't work for QListViews (and QListWidget).
This patch corrects QListViews AdjustToContents
behavior.
[ChangeLog][QtWidgets][QListView] A more correct implementation
of QListView::viewportSizeHint has been made. That
implies that setting the sizeAdjustPolicy to AdjustToContent
on QListView and QListWidget will now cause the view to
size after the contents and avoid scrollbars.
Pick-to: 6.2
Task-number: QTBUG-58749
Change-Id: I1675115f2348e2fcf0b2c39b451ef337e10eb872
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'tests/auto/widgets')
-rw-r--r-- | tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp b/tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp index 6126503ef4..21c69b652e 100644 --- a/tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp +++ b/tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp @@ -38,6 +38,8 @@ #include <QTimer> #include <QtMath> #include <QProxyStyle> +#include <QVBoxLayout> +#include <QDialog> #include <QtTest/private/qtesthelpers_p.h> #include <QtWidgets/private/qlistview_p.h> @@ -166,6 +168,7 @@ private slots: void taskQTBUG_39902_mutualScrollBars(); void horizontalScrollingByVerticalWheelEvents(); void taskQTBUG_7232_AllowUserToControlSingleStep(); + void taskQTBUG_58749_adjustToContent(); void taskQTBUG_51086_skippingIndexesInSelectedIndexes(); void taskQTBUG_47694_indexOutOfBoundBatchLayout(); void itemAlignment(); @@ -2527,6 +2530,45 @@ void tst_QListView::taskQTBUG_7232_AllowUserToControlSingleStep() QCOMPARE(hStep1, lv.horizontalScrollBar()->singleStep()); } +void tst_QListView::taskQTBUG_58749_adjustToContent() +{ + QStandardItemModel model; + model.setRowCount(20); + model.setColumnCount(1); + const QString rowStr = QStringLiteral("Row number txt:"); + for (int u = 0; u < model.rowCount(); ++u) + model.setData(model.index(u, 0), rowStr + QString::number(u)); + + QDialog w; // It really should work for QWidget, too, but sometimes an event (like move) + // is needed to get the resize triggered. + QVBoxLayout *l = new QVBoxLayout(&w); + l->setSizeConstraint(QLayout::SetFixedSize); + auto *view = new QListView; + view->setModel(&model); + view->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents); + l->addWidget(view); + l->setSizeConstraint(QLayout::SetFixedSize); + w.show(); + QVERIFY(QTest::qWaitForWindowExposed(&w)); + + const QString longText = "Here we have a row text that is somewhat longer ..."; + + QFontMetrics fm(w.font(), &w); + QRect r = fm.boundingRect(model.data(model.index(0, 0)).toString()); + const int longTextWidth = fm.horizontalAdvance(longText); + QVERIFY(w.height() > r.height() * model.rowCount()); + // We have a width longer than the width for the given index data. + QVERIFY(w.width() > r.width()); + // but ... the width should not have a width matching the much longer text. + QVERIFY(w.width() < longTextWidth); + + // use the long text and make sure the width is adjusted. + model.setData(model.index(0, 0), longText); + QApplication::processEvents(); + QVERIFY(w.width() > longTextWidth); + QVERIFY(view->width() >= longTextWidth); +} + void tst_QListView::taskQTBUG_51086_skippingIndexesInSelectedIndexes() { QStandardItemModel data(10, 1); |