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 /examples/widgets/widgets/elidedlabel/elidedlabel.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 'examples/widgets/widgets/elidedlabel/elidedlabel.cpp')
-rw-r--r-- | examples/widgets/widgets/elidedlabel/elidedlabel.cpp | 74 |
1 files changed, 0 insertions, 74 deletions
diff --git a/examples/widgets/widgets/elidedlabel/elidedlabel.cpp b/examples/widgets/widgets/elidedlabel/elidedlabel.cpp deleted file mode 100644 index 68ba133811..0000000000 --- a/examples/widgets/widgets/elidedlabel/elidedlabel.cpp +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "elidedlabel.h" - -#include <QPainter> -#include <QSizePolicy> -#include <QTextLayout> - -//! [0] -ElidedLabel::ElidedLabel(const QString &text, QWidget *parent) - : QFrame(parent) - , elided(false) - , content(text) -{ - setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); -} -//! [0] - -//! [1] -void ElidedLabel::setText(const QString &newText) -{ - content = newText; - update(); -} -//! [1] - -//! [2] -void ElidedLabel::paintEvent(QPaintEvent *event) -{ - QFrame::paintEvent(event); - - QPainter painter(this); - QFontMetrics fontMetrics = painter.fontMetrics(); - - bool didElide = false; - int lineSpacing = fontMetrics.lineSpacing(); - int y = 0; - - QTextLayout textLayout(content, painter.font()); - textLayout.beginLayout(); - forever { - QTextLine line = textLayout.createLine(); - - if (!line.isValid()) - break; - - line.setLineWidth(width()); - int nextLineY = y + lineSpacing; - - if (height() >= nextLineY + lineSpacing) { - line.draw(&painter, QPoint(0, y)); - y = nextLineY; - //! [2] - //! [3] - } else { - QString lastLine = content.mid(line.textStart()); - QString elidedLastLine = fontMetrics.elidedText(lastLine, Qt::ElideRight, width()); - painter.drawText(QPoint(0, y + fontMetrics.ascent()), elidedLastLine); - line = textLayout.createLine(); - didElide = line.isValid(); - break; - } - } - textLayout.endLayout(); - //! [3] - - //! [4] - if (didElide != elided) { - elided = didElide; - emit elisionChanged(didElide); - } -} -//! [4] |