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
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
19{
20
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
51painter.begin(this);
56textLayout.draw(&painter, QPoint(0,0));
57
58painter.setBrush(QBrush(QColor("#a6ce39")));
60painter.drawEllipse(QRectF(-radius, margin, 2*radius, 2*radius));
61painter.end();
63
65}
66} // plaintextlayout
\inmodule QtGui
Definition qbrush.h:30
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
\reentrant \inmodule QtGui
\reentrant
Definition qfont.h:22
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:486
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
void setPen(const QColor &color)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool begin(QPaintDevice *)
Begins painting the paint device and returns true if successful; otherwise returns false.
void drawEllipse(const QRectF &r)
Draws the ellipse defined by the given rectangle.
void setBrush(const QBrush &brush)
Sets the painter's brush to the given brush.
@ Antialiasing
Definition qpainter.h:52
bool end()
Ends painting.
void fillRect(const QRectF &, const QBrush &)
Fills the given rectangle with the brush specified.
void setRenderHint(RenderHint hint, bool on=true)
Sets the given render hint on the painter if on is true; otherwise clears the render hint.
\inmodule QtGui
Definition qpen.h:28
\inmodule QtCore\reentrant
Definition qpoint.h:217
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore\reentrant
Definition qrect.h:484
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\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 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
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
int width
the width of the widget excluding any window frame
Definition qwidget.h:114
int height
the height of the widget excluding any window frame
Definition qwidget.h:115
QRect rect
the internal geometry of the widget excluding any window frame
Definition qwidget.h:116
virtual void paintEvent(QPaintEvent *event)
This event handler can be reimplemented in a subclass to receive paint events passed in event.
Definition qwidget.cpp:9783
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
@ white
Definition qnamespace.h:31
@ black
Definition qnamespace.h:30
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLint GLint GLint GLint GLint x
[0]
GLuint GLfloat GLfloat GLfloat x1
GLint y
struct _cl_event * event
GLfixed GLfixed x2
#define Q_UNUSED(x)
double qreal
Definition qtypes.h:187
QPainter painter(this)
[7]