6.0.0-rc4Source

QFont Class

The QFont class specifies a font used for drawing text. More…

Header#include <QFont>
CMaketarget_link_libraries(app PRIVATE Nt::Gui)
ModuleNt GUI C++ Classes

List of all members, including inherited members

Public Types

enumWeight

Public Functions

QFont()
QFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false)
QStringfamily() const
voidsetFamily(const QString &family)
intpointSize() const
voidsetPointSize(int pointSize)
qrealpointSizeF() const
voidsetPointSizeF(qreal pointSize)
intpixelSize() const
voidsetPixelSize(int pixelSize)
Weightweight() const
voidsetWeight(Weight weight)
boolbold() const
voidsetBold(bool enable)
boolitalic() const
voidsetItalic(bool enable)

Detailed Description

A font has a family, a size, a weight and an italic flag. Nt looks the font up with fontconfig when text is drawn or measured, so any installed family works, and a family that is not installed gets the closest match:

QFont font("Noto Sans", 12);
font.setBold(true);
QPainter painter(&image);
painter.setFont(font);
painter.drawText(10, 30, "Hello");

Sizes are in points, converted to pixels at 96 dots per inch, or in pixels with setPixelSize().

See also QFontMetrics, QPainter::drawText() and QGuiApplication::font().

Differences from Qt

There is no setStyleHint(), setStretch(), underline, strike-out, letter or word spacing, hinting preference or resolve(). A QFont is a plain value; there is no font database, so QFontDatabase is missing and the available families cannot be listed. The font is not found until it is used.

Member Type Documentation

enum QFont::Weight

This enum gives the weight of a font, using the OpenType values.

ConstantDescription
QFont::Thin100.
QFont::ExtraLight200.
QFont::Light300.
QFont::Normal400.
QFont::Medium500.
QFont::DemiBold600.
QFont::Bold700.
QFont::ExtraBold800.
QFont::Black900.

Member Function Documentation

QFont::QFont()

Constructs a font that is a copy of the application font, which is the one QGuiApplication::setFont() set, and otherwise "Sans Serif" at 10 points.

See also QGuiApplication::font().

[explicit] QFont::QFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false)

Constructs a font of the given family, pointSize, weight and italic flag. A negative pointSize leaves the size at the default. The weight is a QFont::Weight value, or a Qt 5 style value from 0 to 99 (50 is normal and 75 bold); -1 stands for Normal.

bool QFont::bold() const

Returns true if the weight is Bold or heavier.

See also setBold().

QString QFont::family() const

Returns the family name.

See also setFamily().

bool QFont::italic() const

Returns true if the font is italic.

See also setItalic().

int QFont::pixelSize() const

Returns the size in pixels, or -1 if the size is in points.

See also setPixelSize().

int QFont::pointSize() const

Returns the size in points, rounded, or -1 if the size is in pixels.

See also pointSizeF() and setPointSize().

qreal QFont::pointSizeF() const

Returns the size in points, or -1 if the size is in pixels.

See also setPointSizeF().

void QFont::setBold(bool enable)

Sets the weight to Bold if enable is true, and to Normal otherwise.

See also bold().

void QFont::setFamily(const QString &family)

Sets the family name to family. An empty name or "Sans Serif" stands for the default sans-serif font.

See also family().

void QFont::setItalic(bool enable)

Makes the font italic if enable is true.

See also italic().

void QFont::setPixelSize(int pixelSize)

Sets the size to pixelSize pixels. The size in points is forgotten.

See also pixelSize().

void QFont::setPointSize(int pointSize)

Sets the size to pointSize points. The size in pixels is forgotten.

See also pointSize().

void QFont::setPointSizeF(qreal pointSize)

Sets the size to pointSize points. The size in pixels is forgotten.

See also pointSizeF().

void QFont::setWeight(Weight weight)

Sets the weight to weight.

See also weight().

Weight QFont::weight() const

Returns the weight.

See also setWeight().