diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-02-08 08:26:59 +0100 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-02-09 00:23:33 +0100 |
commit | 55f2b448b0aec4e9a8fd3896b2a40f37e939ecb3 (patch) | |
tree | 68a01e22a34672898ee3010c7b1a563b4d869998 /src/gui/doc/snippets/code/src_gui_text_qtextlayout.cpp | |
parent | 31518f1a4e3c4a7c77ae2e008e903a849ba75dd6 (diff) |
Turn elidedlabel example into a code snippet
The example is 90% boiler plate for subclassing QFrame and providing
a bit of GUI to change the size of the label using sliders. The
interesting bit is a block of 25 lines of code, so turn those into a
snippet and add that to the QTextLayout overview documentation.
Fixes: QTBUG-111011
Pick-to: 6.5
Change-Id: I6e97b2ea47b553c8d998ad185cfac006721ef7ee
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
Diffstat (limited to 'src/gui/doc/snippets/code/src_gui_text_qtextlayout.cpp')
-rw-r--r-- | src/gui/doc/snippets/code/src_gui_text_qtextlayout.cpp | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/src/gui/doc/snippets/code/src_gui_text_qtextlayout.cpp b/src/gui/doc/snippets/code/src_gui_text_qtextlayout.cpp index 2602c2ced0..70ec6b01ea 100644 --- a/src/gui/doc/snippets/code/src_gui_text_qtextlayout.cpp +++ b/src/gui/doc/snippets/code/src_gui_text_qtextlayout.cpp @@ -10,6 +10,7 @@ namespace src_gui_text_qtextlayout { struct Wrapper : public QPaintDevice { void wrapper1(); + void elided(); }; QTextLayout textLayout; @@ -24,7 +25,7 @@ int leading = fontMetrics.leading(); qreal height = 0; textLayout.setCacheEnabled(true); textLayout.beginLayout(); -while (1) { +while (true) { QTextLine line = textLayout.createLine(); if (!line.isValid()) break; @@ -49,4 +50,41 @@ textLayout.draw(&painter, QPoint(0, 0)); } // Wrapper::wrapper1 +void Wrapper::elided() { + +QString content; + +//! [elided] +QPainter painter(this); +QFontMetrics fontMetrics = painter.fontMetrics(); + +int lineSpacing = fontMetrics.lineSpacing(); +int y = 0; + +QTextLayout textLayout(content, painter.font()); +textLayout.beginLayout(); +while (true) { + QTextLine line = textLayout.createLine(); + + if (!line.isValid()) + break; + + line.setLineWidth(width()); + const int nextLineY = y + lineSpacing; + + if (height() >= nextLineY + lineSpacing) { + line.draw(&painter, QPoint(0, y)); + y = nextLineY; + } else { + const QString lastLine = content.mid(line.textStart()); + const QString elidedLastLine = fontMetrics.elidedText(lastLine, Qt::ElideRight, width()); + painter.drawText(QPoint(0, y + fontMetrics.ascent()), elidedLastLine); + line = textLayout.createLine(); + break; + } +} +textLayout.endLayout(); +//! [elided] +} + } // src_gui_text_qtextlayout |