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

相关推荐
xcyxiner16 小时前
DicomViewer (目录调整) 2
qt
A小辣椒18 小时前
TShark:Wireshark CLI 功能
linux
xcyxiner18 小时前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
A小辣椒1 天前
TShark:基础知识
linux
AlfredZhao1 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao2 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334662 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪2 天前
linux 拷贝文件或目录到指定的位置
linux
大树883 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠3 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql