Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
window.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 <QPainter>
4#include <QTextLayout>
5#include <QWidget>
6
7namespace plaintextlayout {
8class Window : public QWidget
9{
10protected:
11 void paintEvent(QPaintEvent *event) override;
12
13private:
14 QFont font;
15 QString text;
16};
17
18void Window::paintEvent(QPaintEvent *event)
19{
20
21//! [0]
22QTextLayout textLayout(text, font);
23qreal margin = 10;
24qreal radius = qMin(width()/2.0, height()/2.0) - margin;
25QFontMetrics fm(font);
26
27qreal lineHeight = fm.height();
28qreal y = 0;
29
30textLayout.beginLayout();
31
32while (1) {
33 // create a new line
34 QTextLine line = textLayout.createLine();
35 if (!line.isValid())
36 break;
37
38 qreal x1 = qMax(0.0, pow(pow(radius,2)-pow(radius-y,2), 0.5));
39 qreal x2 = qMax(0.0, pow(pow(radius,2)-pow(radius-(y+lineHeight),2), 0.5));
40 qreal x = qMax(x1, x2) + margin;
41 qreal lineWidth = (width() - margin) - x;
42
43 line.setLineWidth(lineWidth);
44 line.setPosition(QPointF(x, margin+y));
45 y += line.height();
46}
47
48textLayout.endLayout();
49
50QPainter painter;
51painter.begin(this);
52painter.setRenderHint(QPainter::Antialiasing);
53painter.fillRect(rect(), Qt::white);
54painter.setBrush(QBrush(Qt::black));
55painter.setPen(QPen(Qt::black));
56textLayout.draw(&painter, QPoint(0,0));
57
58painter.setBrush(QBrush(QColor("#a6ce39")));
59painter.setPen(QPen(Qt::black));
60painter.drawEllipse(QRectF(-radius, margin, 2*radius, 2*radius));
61painter.end();
62//! [0]
63
64Q_UNUSED(event);
65}
66} // plaintextlayout
void paintEvent(QPaintEvent *event) override
This event handler can be reimplemented in a subclass to receive paint events passed in event.
Definition window.cpp:18