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、重新编译工程,可以正常显示中文了!

相关推荐
DaxiaLeeSuper11 分钟前
Prometheus+Grafana+node_exporter监控linux服务器资源的方案
linux·grafana·prometheus
尽兴-1 小时前
如何将多个.sql文件合并成一个:Windows和Linux/Mac详细指南
linux·数据库·windows·sql·macos
kfepiza1 小时前
Netplan 中 bridges、bonds、ethernets、vlans 之间的关系 笔记250711
linux·tcp/ip·shell
小小不董2 小时前
深入理解oracle ADG和RAC
linux·服务器·数据库·oracle·dba
宇钶宇夕3 小时前
SIMATIC S7-1200的以太网通信能力:协议与资源详细解析
运维·服务器·数据库·程序人生·自动化
杰夫贾维斯3 小时前
CentOS Linux 8 的系统部署 Qwen2.5-7B -Instruct-AWQ
linux·运维·人工智能·机器学习·centos
kfepiza3 小时前
Netplan 配置网桥(Bridge)的模板笔记250711
linux·tcp/ip·ubuntu
kfepiza4 小时前
用Netplan配置网桥bridge笔记250711
linux·ubuntu·debian
CodeWithMe4 小时前
【Note】Linux Kernel 实时技术深入:详解 PREEMPT_RT 与 Xenomai
linux·运维·服务器