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

相关推荐
森G10 分钟前
20、元对象系统---------QT基础
qt
秃头摸鱼侠26 分钟前
OpenClaw + MCP 实战:从 0 搭建可复用自动化工作流
运维·自动化
IT研究所26 分钟前
从工单到智能分析:AIGC运维助手应用价值
大数据·运维·数据库·人工智能·科技·低代码·自动化
西安小哥27 分钟前
Linux操作系统运维命令大全
linux·运维·服务器
Laurence28 分钟前
CMake 报错 Failed to find required Qt component WebEngineWidgets
qt·webengine·cmake·找不到
习惯就好zz32 分钟前
Qt Quick 系统托盘完整实践
开发语言·qt·qml·系统托盘·system tray·qapplication·qguiapplication
笨笨马甲33 分钟前
Qt集成OpenCV
开发语言·qt
笨笨马甲33 分钟前
Qt 工业机器视觉开发
开发语言·qt
小灰灰搞电子1 小时前
Qt 打印输出:printf与qDebug的区别
开发语言·qt
叁金Coder1 小时前
【Centos8 环境下 X86 版本 docker-29.1.3 的安装配置】
运维·docker·容器