summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorDavid Faure <david.faure@kdab.com>2022-05-16 23:58:30 +0200
committerDavid Faure <david.faure@kdab.com>2022-05-17 22:37:40 +0200
commitbb2f4d08d9d138e4f70d6d6db46e24e34500becc (patch)
tree94a38a14157c6b3e0cbaa41692364b45584d29ca /tests
parent7fa17b5f7998926c8f8650694a977eb7a24ae191 (diff)
QTextDocument/QGraphicsTextItem: skip layout in setTextWidth(0)
In a QGraphicsTextItem without a width yet, there's no need to do any layouting. The use case is obviously items with an app-defined size, not the default where text items adapt to their contents. Results: 0.065 msecs to create a QGraphicsTextItem with some text (layouted) 0.036 msecs to set everything up in a QGraphicsTextItem with 0 width QTextEdit was abusing the width 0 to mean "no wrap, width comes from contents", but since the value -1 means that already in QTextDocument, QTextEdit now uses a width of -1 for that meaning. Change-Id: I67ad59c305e5dd34830886e4e6c56dde03c93668 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp5
-rw-r--r--tests/benchmarks/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp19
2 files changed, 22 insertions, 2 deletions
diff --git a/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp b/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp
index bc533df48a..72d2cc7aa7 100644
--- a/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp
+++ b/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp
@@ -1194,8 +1194,9 @@ void tst_QTextEdit::undoRedoShouldRepositionTextEditCursor()
void tst_QTextEdit::lineWrapModes()
{
ed->setLineWrapMode(QTextEdit::NoWrap);
- // NoWrap at the same time as having all lines that are all left aligned means we optimize to only layout once. The effect is that the width is always 0
- QCOMPARE(ed->document()->pageSize().width(), qreal(0));
+ // NoWrap at the same time as having all lines that are all left aligned means we optimize to
+ // only layout once. The effect is that the width is always -1
+ QCOMPARE(ed->document()->pageSize().width(), qreal(-1));
QTextCursor cursor = QTextCursor(ed->document());
cursor.insertText(QString("A simple line"));
diff --git a/tests/benchmarks/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/benchmarks/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp
index fd74a7b3b9..536d366b63 100644
--- a/tests/benchmarks/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp
+++ b/tests/benchmarks/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp
@@ -5,6 +5,7 @@
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QGraphicsView>
+#include <QTextDocument>
class tst_QGraphicsItem : public QObject
{
@@ -33,6 +34,7 @@ private slots:
void shear();
void translate();
void createTextItem();
+ void createTextItemZeroWidth();
};
tst_QGraphicsItem::tst_QGraphicsItem()
@@ -216,5 +218,22 @@ void tst_QGraphicsItem::createTextItem()
}
}
+void tst_QGraphicsItem::createTextItemZeroWidth()
+{
+ // Ensure QFontDatabase loaded the font beforehand
+ QFontInfo(qApp->font()).family();
+ const QString text = "This is some text";
+ QBENCHMARK {
+ QGraphicsTextItem item;
+ item.document()->setTextWidth(0);
+ // Prepare everything
+ item.setPlainText(text);
+ QTextOption option = item.document()->defaultTextOption();
+ option.setAlignment(Qt::AlignHCenter);
+ item.document()->setDefaultTextOption(option);
+ // And (in a real app) set actual text width here
+ }
+}
+
QTEST_MAIN(tst_QGraphicsItem)
#include "tst_qgraphicsitem.moc"