编译qt时。configure 添加选项 -qt-freetype
代码增加:
//设置中文字体 a.setFont(QFont("wenquanyi", 16)); //设置中文编码 #if _MSC_VER QTextCodec *codec = QTextCodec::codecForName("GBK"); #else QTextCodec *codec = QTextCodec::codecForName("UTF-8"); #endif #if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) QTextCodec::setCodecForLocale(codec); QTextCodec::setCodecForCStrings(codec); QTextCodec::setCodecForTr(codec); #else QTextCodec::setCodecForLocale(codec); #endif
运行时报错:
InitializingQFontEngineQPF failed for /nfsroot/Qt4.8.6-hisi /lib/fonts/DejaVuSans.ttf
Initializing QFontEngineQPF failed for /nfsroot/Qt4.8.6-hisi/lib/fonts/DejaVuSans-Bold.ttf
解决办法
lib/fonts/增加wenquanyi_180_50.qpf, wenquanyi_220_50.qpf,删除其他字体。