• 6222阅读
  • 8回复

关于中文显示问题 [复制链接]

上一主题 下一主题
离线kangray
 

只看楼主 倒序阅读 楼主  发表于: 2006-06-14
hi:
  我编写了一些可以从网络数据库获取数据的程序
  但是当数据是中文时,在板子上(我用ARM9)就只能显示方框
  而且还有一件奇怪的事就是我在pc机的linux系统下把一些文件以中文来命名后,下载到板子
上,这时发现文件的名称也不能显示为中文,只是一些???符号
  这两天看了一些qt程序的中文化、国际化之类的帖子,觉得好像不是我的问题所在
  我这种问题与内核有关吗?
  刚接触嵌入式不久,有哪位兄弟能指点一下方向吗
谢谢
离线shiroki

只看该作者 1楼 发表于: 2006-06-14
文件名显示为问号是文件系统的编码问题, 需要在mount的时候加-charset选项
qte程序中的中文显示为方块可能有两个原因,一是中文的textcodec没有编译进qte库, 二是选择的字体不是中文字体。 qte里带的字体只有unifont是有中文的。
--
shiro is White
ki is tree
http://www.cuteqt.com
论坛 http://www.cuteqt.com/bbs
博客 http://www.cuteqt.com/blog
博客镜像: http://sites.cuteqt.com/cuteqt
Linux/Qt/嵌入式讨论群 http://qun.qq.com/air/5699823
离线kangray

只看该作者 2楼 发表于: 2006-06-28
引用第1楼shiroki2006-06-14 12:33发表的“”:
文件名显示为问号是文件系统的编码问题, 需要在mount的时候加-charset选项
qte程序中的中文显示为方块可能有两个原因,一是中文的textcodec没有编译进qte库, 二是选择的字体不是中文字体。 qte里带的字体只有unifont是有中文的。


我选择的字体是unifont,至于中文的textcodec有没有编译进qte库就不知道了
怎么知道中文的textcodec有没有编译进qte库呢?
离线kangray

只看该作者 3楼 发表于: 2006-06-29
原来我的程序运行时显示的是方块
但刚才我试着在运行应用程序时加入-font unifont参数,情况就变了
#./applicationname -font unifont
现在显示的是一些类似于中文的中文,反正是一些没见过的中文,我的程序大概是这样写的

QFont font1(“unifont”,16,50,FALSE,QFont::Unicode);

qApp->setFont(font1);



QString caption=“主窗口“; //使用台式机linux下的GBK输入法输入

QTextCodec *gk_codec=QTextCodec::codecForName(“GBK”);

setCaption(gk_codec->toUnicode(caption));

有谁知道为什么吗?
还有的继续是那个“中文的textcodec没有编译进qte库”的问题,请指教
离线shiroki

只看该作者 4楼 发表于: 2006-06-29
你在程序里调用 QTextcodec::codecFromName("GBK");
看看返回的指针是不是空,就知道GBK这种textcodec有没有编译进去了。 看你的代码应该已经编进codec了亚

其他的看FAQ

你说的类似中文的中文不知道是怎么回事。要不你把那段代码贴出来我们来试试
--
shiro is White
ki is tree
http://www.cuteqt.com
论坛 http://www.cuteqt.com/bbs
博客 http://www.cuteqt.com/blog
博客镜像: http://sites.cuteqt.com/cuteqt
Linux/Qt/嵌入式讨论群 http://qun.qq.com/air/5699823
离线kangray

只看该作者 5楼 发表于: 2006-06-29
我这有如下文件chi.h   chi.cpp   main.cpp
//********************chi.h*******************************
#ifndef FORM1_H
#define FORM1_H

#include <qvariant.h>
#include <qwidget.h>
#include <qtextcodec.h>
#include <qstring.h>
class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;
class QMultiLineEdit;

class Form1 : public QWidget
{
  Q_OBJECT

public:
  Form1( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
  ~Form1();

  QMultiLineEdit* MultiLineEdit1;
 
  QTextCodec* gbk;
  QString mytr(const char*);

};

#endif // FORM1_H

//********************chi.cpp**************************
#include "chi.h"

#include <qmultilineedit.h>
#include <qlayout.h>
#include <qvariant.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qstring.h>

/*
* Constructs a Form1 which is a child of 'parent', with the
* name 'name' and widget flags set to 'f'
*/
Form1::Form1( QWidget* parent, const char* name, WFlags fl )
  : QWidget( parent, name, fl )
{
  if ( !name )
  setName( "Form1" );
  resize( 240, 320 );
  setMaximumSize( QSize( 240, 320 ) );
 
  gbk=QTextCodec::codecForName("GBK");

  QString caption = "鐫€搴婂彛";   //这些中文是在x11的designer输入的,输入时已经是一些乱码
  setCaption( mytr( caption ) );

  MultiLineEdit1 = new QMultiLineEdit( this, "MultiLineEdit1" );
  MultiLineEdit1->setGeometry( QRect( 32, 40, 181, 61 ) );
  MultiLineEdit1->setText( mytr( "脛茫潞脙! China n浣? ) );   //这些中文是在生成chi.cpp后加入的,使用台式机linux下的GBK输入法输入,在台式机的linux下输入时可以显示为正确的中文

 
}

/*
* Destroys the object and frees any allocated resources
*/
Form1::~Form1()
{
  // no need to delete child widgets, Qt does it all for us
}

QString Form1::mytr(const char* chars)
{

  return gbk->toUnicode(chars,strlen(chars));
}


//***************main.cpp***********************
#include "chi.h"
#include <qapplication.h>
#include <qfont.h>

int main( int argc,char **argv)
{
  QApplication a (argc,argv);
  Form1 b;
  a.setMainWidget( &b );

  QFont font1("unifont",16,50,FALSE,QFont::Unicode);
  qApp->setFont(font1);

  b.show();
  return a.exec();

}
我用的qte是2.3.7的,好像太旧了:)
很奇怪,我用designer设计时不能输入中文,是一些乱码
为了实验观察,我在由designer生成的.ui文件转换而得到的chi.cpp文件中的MultiLineEdit1又加入了一些中文,这些中文是使用台式机linux的GBK输入法输入的
先谢谢了
[ 此贴被kangray在2006-06-29 15:56重新编辑 ]
离线shiroki

只看该作者 6楼 发表于: 2006-06-30
designer 保存用的是utf8的编码, 生成的源码里中文也都是utf8的
要显示这个中文,首先需要调qApp->setDefaultCodec("UTF8");
然后把生成的代码里的 QString::fromUtf8去掉

这个是我试出来的,具体的原理尚不清楚
[ 此贴被shiroki在2006-06-30 11:36重新编辑 ]
--
shiro is White
ki is tree
http://www.cuteqt.com
论坛 http://www.cuteqt.com/bbs
博客 http://www.cuteqt.com/blog
博客镜像: http://sites.cuteqt.com/cuteqt
Linux/Qt/嵌入式讨论群 http://qun.qq.com/air/5699823
离线shiroki

只看该作者 7楼 发表于: 2006-06-30
用kate打开源码文件,输入中文后保存。 需要用gbk的codec把这些字符转成unicode才能正确显示。 可见输入法输入的是GBK编码的字符。
[ 此贴被shiroki在2006-06-30 11:37重新编辑 ]
--
shiro is White
ki is tree
http://www.cuteqt.com
论坛 http://www.cuteqt.com/bbs
博客 http://www.cuteqt.com/blog
博客镜像: http://sites.cuteqt.com/cuteqt
Linux/Qt/嵌入式讨论群 http://qun.qq.com/air/5699823
离线kangray

只看该作者 8楼 发表于: 2006-07-04
呵呵
终于可以显示中文了
再次感谢shiroki斑竹的指教:)
快速回复
限100 字节
 
上一个 下一个