QFont Class
The QFont class specifies a font used for drawing text. More…
| Header | #include <QFont> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Gui) |
| Module | Nt GUI C++ Classes |
List of all members, including inherited members
Public Types
| enum | Weight |
Public Functions
| QFont() | |
| QFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false) | |
| QString | family() const |
| void | setFamily(const QString &family) |
| int | pointSize() const |
| void | setPointSize(int pointSize) |
| qreal | pointSizeF() const |
| void | setPointSizeF(qreal pointSize) |
| int | pixelSize() const |
| void | setPixelSize(int pixelSize) |
| Weight | weight() const |
| void | setWeight(Weight weight) |
| bool | bold() const |
| void | setBold(bool enable) |
| bool | italic() const |
| void | setItalic(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.
| Constant | Description |
|---|---|
QFont::Thin | 100. |
QFont::ExtraLight | 200. |
QFont::Light | 300. |
QFont::Normal | 400. |
QFont::Medium | 500. |
QFont::DemiBold | 600. |
QFont::Bold | 700. |
QFont::ExtraBold | 800. |
QFont::Black | 900. |
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().
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().