diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-11-25 12:28:07 +0100 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-12-03 10:57:37 +0100 |
commit | 71cca7d2c29cea0e60403f7cf28b5d659233d914 (patch) | |
tree | 813db3cc603132faac4f9059e085ff0e66ddc0a6 | |
parent | a18ef048374c339023305c63600bd3c4d61b858b (diff) |
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
Change-Id: Idf4ad015938abb8d3e599e9a58e002f29c0067be
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
(cherry picked from commit 97cfd4940157817dd761fd02f2e6da4afc5b4a30)
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
-rw-r--r-- | src/gui/text/qtextdocumentlayout.cpp | 1 | ||||
-rw-r--r-- | tests/auto/gui/text/qtexttable/tst_qtexttable.cpp | 61 |
2 files changed, 61 insertions, 1 deletions
diff --git a/src/gui/text/qtextdocumentlayout.cpp b/src/gui/text/qtextdocumentlayout.cpp index 35e840e665..cff40c77c3 100644 --- a/src/gui/text/qtextdocumentlayout.cpp +++ b/src/gui/text/qtextdocumentlayout.cpp @@ -2364,6 +2364,7 @@ QTextLayoutStruct QTextDocumentLayoutPrivate::layoutCell(QTextTable *t, const QT // constraint the maximumWidth by the minimum width of the fixed size floats, to // keep them visible layoutStruct.maximumWidth = qMax(layoutStruct.maximumWidth, floatMinWidth); + layoutStruct.minimumWidth = floatMinWidth; // as floats in cells get added to the table's float list but must not affect // floats in other cells we must clear the list here. diff --git a/tests/auto/gui/text/qtexttable/tst_qtexttable.cpp b/tests/auto/gui/text/qtexttable/tst_qtexttable.cpp index b7baf720b3..7d4921203c 100644 --- a/tests/auto/gui/text/qtexttable/tst_qtexttable.cpp +++ b/tests/auto/gui/text/qtexttable/tst_qtexttable.cpp @@ -29,7 +29,7 @@ #include <QtTest/QtTest> - +#include <qbuffer.h> #include <qtextdocument.h> #include <qtextdocumentfragment.h> #include <qtexttable.h> @@ -103,6 +103,9 @@ private slots: #ifndef QT_NO_WIDGETS void columnWidthWithSpans(); + + void columnWidthWithImage_data(); + void columnWidthWithImage(); #endif private: @@ -1304,6 +1307,62 @@ 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("<td><img src='data:image/png;base64,%1'/></td>") + .arg(QString::fromLatin1(imageBytes.toBase64())); + }; + + QTest::addColumn<QString>("leftHtml"); + QTest::addColumn<QString>("rightHtml"); + QTest::addColumn<QSize>("imageSize"); + QTest::addRow("image") + << imageHtml(500, 32) << "<td></td>" << QSize(500, 32); + QTest::addRow("image, text") + << imageHtml(32, 32) << "<td>abc</td>" << QSize(32, 32); + QTest::addRow("image, 100%% text") + << imageHtml(32, 32) << "<td style='background-color: grey' width='100%'>abc</td>" + << QSize(32, 32); + QTest::addRow("image, image") + << imageHtml(256, 32) << imageHtml(256, 32) << QSize(256, 32); +} + +void tst_QTextTable::columnWidthWithImage() +{ + const QString tableTemplate = "<table><tr>%1 %2</tr></table>"; + + 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) |