summaryrefslogtreecommitdiffstats
path: root/src/gui/doc/snippets/code/src_gui_text_qtextlayout.cpp
diff options
context:
space:
mode:
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.cpp40
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