From 4d552815cf370c0d5abbbeb50ff1350b449e7bb2 Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Tue, 4 Oct 2016 12:41:47 +0200 Subject: [PATCH] Fix plain text QStaticText with line breaks The layout isn't actually created until endLayout() or setLineWidth() is called. So in the case where this was not done, the height of the line would be 0, thus multiple lines would be placed on top of each other, at y == 0. [ChangeLog][QtGui][Text] Fixed QStaticText when manually breaking lines and no text width was set. Task-number: QTBUG-56346 Change-Id: I7f6ed6260545882f05fe39b21134315eca7401b9 Reviewed-by: Konstantin Ritt --- src/gui/text/qstatictext.cpp | 2 + .../gui/text/qstatictext/tst_qstatictext.cpp | 55 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/gui/text/qstatictext.cpp b/src/gui/text/qstatictext.cpp index a10071490e..dbc2e6e558 100644 --- a/src/gui/text/qstatictext.cpp +++ b/src/gui/text/qstatictext.cpp @@ -622,6 +622,8 @@ void QStaticTextPrivate::paintText(const QPointF &topLeftPosition, QPainter *p) if (textWidth >= 0.0) line.setLineWidth(textWidth); + else + line.setLineWidth(QFIXED_MAX); height += leading; line.setPosition(QPointF(0.0, height)); height += line.height(); diff --git a/tests/auto/gui/text/qstatictext/tst_qstatictext.cpp b/tests/auto/gui/text/qstatictext/tst_qstatictext.cpp index ec30cc8b67..b3d1b75a42 100644 --- a/tests/auto/gui/text/qstatictext/tst_qstatictext.cpp +++ b/tests/auto/gui/text/qstatictext/tst_qstatictext.cpp @@ -96,6 +96,8 @@ private slots: void textDocumentColor(); #endif + void multiLine(); + private: bool supportsTransformations() const; @@ -854,5 +856,58 @@ void tst_QStaticText::textDocumentColor() } #endif +class TestPaintEngine: public QPaintEngine +{ +public: + void drawTextItem(const QPointF &p, const QTextItem &textItem) Q_DECL_OVERRIDE + { + differentVerticalPositions.insert(qRound(p.y())); + } + + void updateState(const QPaintEngineState &) Q_DECL_OVERRIDE {} + + void drawPolygon(const QPointF *, int , PolygonDrawMode ) Q_DECL_OVERRIDE {} + + bool begin(QPaintDevice *) Q_DECL_OVERRIDE { return true; } + bool end() Q_DECL_OVERRIDE { return true; } + void drawPixmap(const QRectF &, const QPixmap &, const QRectF &) Q_DECL_OVERRIDE {} + Type type() const Q_DECL_OVERRIDE + { + return User; + } + + QSet differentVerticalPositions; +}; + +class TestPixmap: public QPixmap +{ +public: + TestPixmap(int w, int h) : QPixmap(w, h), testPaintEngine(new TestPaintEngine) {} + ~TestPixmap() { delete testPaintEngine; } + + QPaintEngine *paintEngine() const + { + return testPaintEngine; + } + + TestPaintEngine *testPaintEngine; +}; + +void tst_QStaticText::multiLine() +{ + TestPixmap pixmap(100, 100); + + TestPaintEngine *paintEngine = pixmap.testPaintEngine; + + { + QPainter p(&pixmap); + QStaticText text; + text.setText(QLatin1String("line 1") + QChar(QChar::LineSeparator) + QLatin1String("line 2")); + p.drawStaticText(0, 0, text); + } + + QCOMPARE(paintEngine->differentVerticalPositions.size(), 2); +} + QTEST_MAIN(tst_QStaticText) #include "tst_qstatictext.moc"