You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt中QML与C++渲染同一字体显示效果不一致的同步方案咨询

QML与C++字体渲染效果同步问题

使用同一字体配置时,QML与C++代码渲染出的字体显示效果存在明显差异:

QML代码及渲染效果

TextEdit {  
    ...                  
    font: settings.font // font.preferShaping = false 无改善
    ...
    renderType: Text.QtRendering // Text.NativeRendering 也无法正常渲染
}

渲染效果:字体边角圆润

C++代码及渲染效果

QSGNode* Text::updatePaintNode(QSGNode* oldNode, QQuickItem::UpdatePaintNodeData*)
{
    ...
    QImage image(QSize(size, QImage::Format_ARGB32_Premultiplied);
    QPainter painter(&image);
    // painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform); 同样无改善
    image.fill(QColor(0, 0, 0, 0));
    painter.setFont(settings.font());
    painter.drawText(rect, alignment(), text));
    ...
}

渲染效果:字体边角锐利

无效的配置尝试

以下QML配置仍无法修正边角圆润的问题:

font.family: "Segoe UI"
font.pixelSize: 15
font.preferShaping: false

可复现示例代码

import QtQuick 2.15
import QtQuick.Window 2.15

Window {
    width: 640
    height: 640
    visible: true

    Rectangle {
        id: rect
        color: "#303030"
        width: textEdit.contentWidth
        height: textEdit.contentHeight

        TextEdit {
            id: textEdit
            color: "yellow"
            font.family: "Segoe UI"
            font.pointSize: 300
            font.preferShaping: false
            text: "Й"
        }
    }
}

运行效果:字体边角圆润

核心问题

如何同步QML与C++中的字体显示效果?

环境:Windows 10,测试版本为Qt 5.15.8 msvc2019 及 Qt 6.5.0 msvc2019


内容的提问来源于stack exchange,提问作者Vladimir Bershov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 15:38:22