From 97cfd4940157817dd761fd02f2e6da4afc5b4a30 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Thu, 25 Nov 2021 12:28:07 +0100 Subject: Don't let text table cells shrink below their minimum width We calculate the minimum width, but then use it only to make sure that the maximum width is at least as large as it. Without setting the layout struct's minimumWidth as well, table cells can be smaller. Add a test case. Fixes: QTBUG-86671 Fixes: QTBUG-97463 Pick-to: 6.2 5.15 Change-Id: Idf4ad015938abb8d3e599e9a58e002f29c0067be Reviewed-by: Allan Sandfeld Jensen --- tests/auto/gui/text/qtexttable/tst_qtexttable.cpp | 61 ++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) (limited to 'tests/auto') diff --git a/tests/auto/gui/text/qtexttable/tst_qtexttable.cpp b/tests/auto/gui/text/qtexttable/tst_qtexttable.cpp index 5d8d47491a..8575da04bb 100644 --- a/tests/auto/gui/text/qtexttable/tst_qtexttable.cpp +++ b/tests/auto/gui/text/qtexttable/tst_qtexttable.cpp @@ -29,7 +29,7 @@ #include - +#include #include #include #include @@ -103,6 +103,9 @@ private slots: #ifndef QT_NO_WIDGETS void columnWidthWithSpans(); + + void columnWidthWithImage_data(); + void columnWidthWithImage(); #endif private: @@ -1304,7 +1307,63 @@ void tst_QTextTable::columnWidthWithSpans() const QRectF afterRect = table->document()->documentLayout()->blockBoundingRect(block); QCOMPARE(afterRect, beforeRect); } + +void tst_QTextTable::columnWidthWithImage_data() +{ + const auto imageHtml = [](int width, int height) { + QImage image(width, height, QImage::Format_RGB32); + image.fill(Qt::red); + QByteArray imageBytes; + QBuffer buffer(&imageBytes); + buffer.open(QIODevice::WriteOnly); + image.save(&buffer, "png"); + return QString("").arg(imageBytes.toBase64()); + }; + + QTest::addColumn("leftHtml"); + QTest::addColumn("rightHtml"); + QTest::addColumn("imageSize"); + QTest::addRow("image") + << imageHtml(500, 32) << "" << QSize(500, 32); + QTest::addRow("image, text") + << imageHtml(32, 32) << "abc" << QSize(32, 32); + QTest::addRow("image, 100%% text") + << imageHtml(32, 32) << "abc" + << QSize(32, 32); + QTest::addRow("image, image") + << imageHtml(256, 32) << imageHtml(256, 32) << QSize(256, 32); +} + +void tst_QTextTable::columnWidthWithImage() +{ + const QString tableTemplate = "%1 %2
"; + + QFETCH(QString, leftHtml); + QFETCH(QString, rightHtml); + QFETCH(QSize, imageSize); + + QTextDocument doc; + doc.setHtml(tableTemplate.arg(leftHtml).arg(rightHtml)); + QTextEdit textEdit; + textEdit.setDocument(&doc); + textEdit.show(); + QVERIFY(QTest::qWaitForWindowExposed(&textEdit)); + + QTextCursor cursor(doc.firstBlock()); + cursor.movePosition(QTextCursor::Right); + + QTextTable *currentTable = cursor.currentTable(); + QVERIFY(currentTable); + + QTextBlock block = currentTable->cellAt(0, 0).firstCursorPosition().block(); + const QRectF leftRect = currentTable->document()->documentLayout()->blockBoundingRect(block); + block = currentTable->cellAt(0, 1).firstCursorPosition().block(); + const QRectF rightRect = currentTable->document()->documentLayout()->blockBoundingRect(block); + QCOMPARE(leftRect.size().toSize(), imageSize); + QVERIFY(rightRect.left() > leftRect.right()); +} #endif + QTEST_MAIN(tst_QTextTable) #include "tst_qtexttable.moc" -- cgit v1.2.3