Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
src_gui_text_qtextlayout.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3#include <QFont>
4#include <QFontMetrics>
5#include <QPainter>
6#include <QTextLayout>
7#include <QTextLine>
8
10struct Wrapper : public QPaintDevice
11{
12 void wrapper1();
13 void elided();
14};
16
17
18void wrapper0() {
19qreal lineWidth = 0;
20QFont aFont;
22
24int leading = fontMetrics.leading();
25qreal height = 0;
28while (true) {
30 if (!line.isValid())
31 break;
32
33 line.setLineWidth(lineWidth);
34 height += leading;
35 line.setPosition(QPointF(0, height));
36 height += line.height();
37}
40
41} // wrapper0
42
43
45
47QPainter painter(this);
50
51} // Wrapper::wrapper1
52
54
55QString content;
56
58QPainter painter(this);
60
61int lineSpacing = fontMetrics.lineSpacing();
62int y = 0;
63
66while (true) {
68
69 if (!line.isValid())
70 break;
71
72 line.setLineWidth(width());
73 const int nextLineY = y + lineSpacing;
74
75 if (height() >= nextLineY + lineSpacing) {
76 line.draw(&painter, QPoint(0, y));
77 y = nextLineY;
78 } else {
79 const QString lastLine = content.mid(line.textStart());
80 const QString elidedLastLine = fontMetrics.elidedText(lastLine, Qt::ElideRight, width());
81 painter.drawText(QPoint(0, y + fontMetrics.ascent()), elidedLastLine);
83 break;
84 }
85}
88}
89
90} // src_gui_text_qtextlayout
\reentrant \inmodule QtGui
int lineSpacing() const
Returns the distance from one base line to the next.
\reentrant
Definition qfont.h:22
int width() const
int height() const
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
const QFont & font() const
Returns the currently set font used for drawing text.
QFontMetrics fontMetrics() const
Returns the font metrics for the painter if the painter is active.
void drawText(const QPointF &p, const QString &s)
Draws the given text with the currently defined text direction, beginning at the given position.
\inmodule QtCore\reentrant
Definition qpoint.h:217
\inmodule QtCore\reentrant
Definition qpoint.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString mid(qsizetype position, qsizetype n=-1) const &
Definition qstring.cpp:5300
\reentrant
Definition qtextlayout.h:70
QTextLine createLine()
Returns a new text line to be laid out if there is text to be inserted into the layout; otherwise ret...
void beginLayout()
Begins the layout process.
void setCacheEnabled(bool enable)
Enables caching of the complete layout information if enable is true; otherwise disables layout cachi...
void draw(QPainter *p, const QPointF &pos, const QList< FormatRange > &selections=QList< FormatRange >(), const QRectF &clip=QRectF()) const
Draws the whole layout on the painter p at the position specified by pos.
void endLayout()
Ends the layout process.
\reentrant
fontMetrics
@ ElideRight
Definition qnamespace.h:190
GLint GLsizei GLsizei height
GLint y
double qreal
Definition qtypes.h:187
QPainter painter(this)
[7]