Qt中如何设置自定义QFont字重?
Got it, let's figure out how to get that precise QFont weight you need—since the default 0-99 enum isn't cutting it for 120px or 1.5mm stroke thickness. The core issue here is that QFont's built-in QFont::Weight values are relative, not absolute—their actual stroke thickness depends entirely on the font you're using, so they can't directly map to pixel or millimeter measurements.
Here are three practical solutions to solve this:
1. Use Variable Fonts for Exact Weight Control
Modern variable fonts (like Inter, Roboto Flex, or Montserrat Variable) support fine-grained weight adjustments, and Qt 5.13+ fully supports them. Unlike standard fonts, variable fonts let you use QFont::setWeight() with values from 100 to 900 (some even go 0-1000), which gives you way more control.
To map your target thickness (120px or 1.5mm) to a weight value:
- Check the font's documentation or use a tool like FontForge to find its weight-to-stroke-thickness mapping.
- Calculate the corresponding weight value, then apply it like this:
QFont customFont("Inter Variable"); // Replace with your variable font name customFont.setWeight(600); // Example: if 1.5mm maps to 600 for your font
This gives you the most natural-looking result, as it uses the font's native design variations.
2. Simulate Thick Weight with QPainter Stroke
If you can't use a variable font, you can manually add a stroke to your text to mimic a thicker weight. This works with any font and lets you set an exact pixel or millimeter thickness:
void drawThickText(QPainter* painter, const QString& text, const QPointF& position, qreal targetThickness) { QFont originalFont = painter->font(); painter->setFont(originalFont); // First draw a thick stroke around the text painter->setPen(QPen(Qt::black, targetThickness, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter->drawText(position, text); // Then draw the original text on top to keep the sharp inner edges painter->setPen(Qt::black); painter->drawText(position, text); }
To use millimeters, convert them to pixels first (1mm ≈ 3.7795 pixels, so 1.5mm ≈ 5.67 pixels). This method is quick to implement and matches the "regular vs extended text" difference you mentioned.
3. Scale Font Outlines Directly
For maximum control, you can extract the font's glyph outlines and scale their thickness manually:
void drawScaledOutlineText(QPainter* painter, const QString& text, const QPointF& position, qreal scaleFactor) { QFont customFont("Your Font"); QFontMetricsF fontMetrics(customFont); QPainterPath textPath = fontMetrics.path(text); // Create a stroked version of the path and scale it QPainterPathStroker stroker; stroker.setWidth(2); // Base stroke width to scale QPainterPath thickPath = stroker.createStroke(textPath); thickPath = thickPath.scaled(1, scaleFactor); // Adjust scale to hit your target thickness painter->fillPath(thickPath, Qt::black); }
You'll need to tweak the scaleFactor to match your desired 120px or 1.5mm thickness, but this gives you full control over the stroke shape.
内容的提问来源于stack exchange,提问作者user18737700

