summaryrefslogtreecommitdiffstats
path: root/src/gui/doc/snippets/code/src_gui_text_qtextlayout.cpp
blob: 2602c2ced0ecc884b8a22eaaf548553ea7253cd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QFont>
#include <QFontMetrics>
#include <QPainter>
#include <QTextLayout>
#include <QTextLine>

namespace src_gui_text_qtextlayout {
struct Wrapper : public QPaintDevice
{
    void wrapper1();
};
QTextLayout textLayout;


void wrapper0() {
qreal lineWidth = 0;
QFont aFont;
QFontMetrics fontMetrics(aFont);

//! [0]
int leading = fontMetrics.leading();
qreal height = 0;
textLayout.setCacheEnabled(true);
textLayout.beginLayout();
while (1) {
    QTextLine line = textLayout.createLine();
    if (!line.isValid())
        break;

    line.setLineWidth(lineWidth);
    height += leading;
    line.setPosition(QPointF(0, height));
    height += line.height();
}
textLayout.endLayout();
//! [0]

} // wrapper0


void Wrapper::wrapper1() {

//! [1]
QPainter painter(this);
textLayout.draw(&painter, QPoint(0, 0));
//! [1]

} // Wrapper::wrapper1

} // src_gui_text_qtextlayout