查看完整版本: [-- QPainter drawText设置字体中英文有差异 --]

QTCN开发网 -> Qt基础编程 -> QPainter drawText设置字体中英文有差异 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

lwei24 2022-05-27 17:51

QPainter drawText设置字体中英文有差异

如题,在QPainter自绘的过程中,自己设置了第三方字体,中文的并没与设置成功,但英文和数字却设置成功了。
具体代码如下:


  1.     //构造函数
      
        QStringList fontName = QFontDatabase::applicationFontFamilies(fontId);

        if (fontName.count() > 0)
        {
            iFont = QFont(fontName.at(0));// iFont私有成员
        }
        else
        {
            qDebug() << "load XenoisSansPro-Bold.ttf error";
        }

       //重绘
    void CustomRectangle::paintEvent(QPaintEvent *event)
    {
        Q_UNUSED(event);

        float percent = 0.612;
        QRectF rectangle(10.0, 20.0, 670.0, 26.0);
        painter.drawRect(rectangle);
        painter.fillRect(rectangle, QColor("#293140"));
        QRectF rectUsed(10.0, 20.0, 670.0*percent, 26.0);
        painter.fillRect(rectUsed, QColor("#2F88F0"));
        QRectF rectUnUsed(10.0 + 670.0*percent, 20.0, 670.0*(1-percent), 26.0);
        painter.fillRect(rectUnUsed, QColor(Qt::red));
        QFont font;
        font.setFamily("XenoisSansPro-Bold");
        font.setPixelSize(14);
        painter.setFont(font); //设置XenoisSansPro-Bold字体
        QPen pen;
        pen.setColor(QColor(Qt::white));
        painter.setPen(pen);
        QRectF rectText(10.0 + 10.0, 20.0, 670.0, 26.0);

        //XenoisSansPro-Bold字体数字和英文设置成功,中文字体未变
        painter.drawText(rectText, Qt::AlignLeft | Qt::AlignVCenter, QString("61.2 GB 已用/38.8 GB 可使用"));
        //XenoisSansPro-Bold字体设置成功
        //painter.drawText(rectText, Qt::AlignLeft | Qt::AlignVCenter, QString("61.2 GB Used/38.8 GB UnUsed"));
    }



如上,为什么有中文的时候,中文的字体没有和英文或数字同意加载成为XenoisSansPro-Bold字体呢?
请问各位大佬们,为啥会有中文字体未设置成功呢?各位大佬们帮忙看看,给点意见,小弟在此多谢了!

liudianwu 2022-05-28 10:07
前提是你的字体要支持中文,很多字体只有英文数字,体积很小。

lwei24 2022-05-30 16:24
liudianwu:前提是你的字体要支持中文,很多字体只有英文数字,体积很小。 (2022-05-28 10:07) 

好吧,多谢大佬指点!


查看完整版本: [-- QPainter drawText设置字体中英文有差异 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled