Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
9
namespace
src_gui_text_qtextlayout
{
10
struct
Wrapper
:
public
QPaintDevice
11
{
12
void
wrapper1
();
13
void
elided
();
14
};
15
QTextLayout
textLayout
;
16
17
18
void
wrapper0
() {
19
qreal lineWidth = 0;
20
QFont aFont;
21
QFontMetrics fontMetrics(aFont);
22
23
//! [0]
24
int
leading = fontMetrics.leading();
25
qreal height = 0;
26
textLayout
.setCacheEnabled(
true
);
27
textLayout
.beginLayout();
28
while
(
true
) {
29
QTextLine line = textLayout.createLine();
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
}
38
textLayout
.endLayout();
39
//! [0]
40
41
}
// wrapper0
42
43
44
void
Wrapper
::
wrapper1
() {
45
46
//! [1]
47
QPainter painter(
this
);
48
textLayout
.draw(&painter, QPoint(0, 0));
49
//! [1]
50
51
}
// Wrapper::wrapper1
52
53
void
Wrapper
::
elided
() {
54
55
QString content;
56
57
//! [elided]
58
QPainter painter(
this
);
59
QFontMetrics fontMetrics = painter.fontMetrics();
60
61
int
lineSpacing = fontMetrics.lineSpacing();
62
int
y = 0;
63
64
QTextLayout textLayout(content, painter.font());
65
textLayout.beginLayout();
66
while
(
true
) {
67
QTextLine line = textLayout.createLine();
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);
82
line = textLayout.createLine();
83
break
;
84
}
85
}
86
textLayout.endLayout();
87
//! [elided]
88
}
89
90
}
// src_gui_text_qtextlayout
src_gui_text_qtextlayout
Definition
src_gui_text_qtextlayout.cpp:9
src_gui_text_qtextlayout::textLayout
QTextLayout textLayout
Definition
src_gui_text_qtextlayout.cpp:15
src_gui_text_qtextlayout::wrapper0
void wrapper0()
Definition
src_gui_text_qtextlayout.cpp:18
src_gui_text_qtextlayout::Wrapper
Definition
src_gui_text_qtextlayout.cpp:11
src_gui_text_qtextlayout::Wrapper::wrapper1
void wrapper1()
Definition
src_gui_text_qtextlayout.cpp:44
src_gui_text_qtextlayout::Wrapper::elided
void elided()
Definition
src_gui_text_qtextlayout.cpp:53
qtbase
src
gui
doc
snippets
code
src_gui_text_qtextlayout.cpp
Generated on
for Qt by
1.18.0