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

相关推荐
zzzzzz31012 小时前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
xcyxiner12 小时前
DicomViewer (dcmtk读取dcm文件)5
qt
XIAOHEZIcode12 小时前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
xcyxiner1 天前
DicomViewer (后台线程处理文件)4
qt
用户0328472220701 天前
如何搭建本地yum源(上)
运维
xcyxiner1 天前
DicomViewer (添加模型类)3
qt
xcyxiner2 天前
DicomViewer (目录调整) 2
qt
A小辣椒2 天前
TShark:Wireshark CLI 功能
linux
xcyxiner2 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
A小辣椒2 天前
TShark:基础知识
linux