Linux下qt程序缺少中文字库,中文显示为框框

现象

Linux下qt5.9编译程序,运行时候界面上的中文显示一个一个的框框。

如图

原因

开发板里缺少中文字库或者qt字库环境未正常配置导致的。

解决方法一:

如果系统中存在中文字库,一般是在/usr/share/下有一个fonts文件夹

配置qt中文字库路径,在/etc/profile文件末尾添加

bash 复制代码
export QT_QPA_FONTDIR=/usr/share/fonts
解决方法二:

如果系统中不存在中文字库

1、去以下网址下载一个中文字库。

bash 复制代码
http://wenq.org/wqy2/index.cgi?BitmapSong

2、将下载之后的.prc结尾的文件全部传入到开发板中(放到这个路径下:/usr/lib/fonts/)

3、打开你的qt工程,在main.cpp里面添加对中文字库的支持

bash 复制代码
#include "mainwindow.h"
#include <QApplication>
#include <QFont>
#include <QFontDatabase>
int main(int argc,char *argv[])
{
	QApplication a(argc,argv);/添加中文字库
	int nlndex = QFontDatabase::addApplicationFont("/usr/lib/fonts/wenquanyi_9pt.pcf");
	QStringList strlist(QFontDatabase::applicationFontFamilies(nlndex));
	if(strlist.count()> 0)
	{
		QFont fontthis(strlist.at(0));
		a.setFont(fontthis);
	}
	MainWindow w;
	w.show();
	return a.exec();
}

4、重新编译工程,可以正常显示中文了!

相关推荐
Sokach10154 小时前
Linux Shell 脚本从零到能用:一个新手的一天学习总结
linux
SelectDB19 小时前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台
运维·后端·自动化运维
AlfredZhao19 小时前
Docker 容器时区不对,`timedatectl` 不存在怎么办?
linux·timezone
zzzzzz3102 天前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
xcyxiner2 天前
DicomViewer (dcmtk读取dcm文件)5
qt
XIAOHEZIcode2 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
xcyxiner3 天前
DicomViewer (后台线程处理文件)4
qt
用户0328472220703 天前
如何搭建本地yum源(上)
运维
xcyxiner3 天前
DicomViewer (添加模型类)3
qt
xcyxiner4 天前
DicomViewer (目录调整) 2
qt