zzh2004linux 发表于 2005-7-6 16:40:32

qt4 无法显示中文

我的系统是xp sp2
软件是mingw 4.1.0    qt4 for mingw
hello world无法显示中文(英文能正常显示),中文地方都是...........我都无法形容那是什么了,就象是脏玻璃上的斑点。

sejishikong 发表于 2005-7-6 17:44:05

qtextcodec用了?
或者tr方法也不行?
刚出来的东西,没必要追这么快吧。

yunfan 发表于 2005-7-6 18:37:42

这个,放这里不合适吧。

显示中文肯定没问题的, 我试过了。
你把你代码贴出来看看吧。

zzh2004linux 发表于 2005-7-6 19:09:49

#include <QApplication>
    #include <QPushButton>

    int main(int argc, char *argv[])
    {
      QApplication app(argc, argv);

      QPushButton hello("你好");
      hello.resize(100, 30);

      hello.show();
      return app.exec();
    }
编译后,中文是乱码,如果加tr那就应该是
#include <QApplication>
    #include <QPushButton>

    int main(int argc, char *argv[])
    {
      QApplication app(argc, argv);

      QPushButton hello(tr("你好"));
      hello.resize(100, 30);

      hello.show();
      return app.exec();
    }
对不?可连make都过不去呀,提示:
Setting up a MinGW/Qt only environment...
-- QTDIR set to d:\Qt\4.0.0
-- PATH set to d:\Qt\4.0.0\bin
-- Adding d:\MinGW\bin to PATH
-- Adding C:\WINDOWS\System32 to PATH
-- QMAKESPEC set to win32-g++

d:\Qt\4.0.0>cd d:\123

D:\123>qmake -project

D:\123>qmake

D:\123>make
mingw32-make -f Makefile.Release
mingw32-make: Entering directory `D:/123'
g++ -c -O2 -O2 -frtti -fexceptions -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_D
LL -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN
-I"D:/Qt/4.0.0/include/QtGui" -I"D:/Qt/4.0.0/include/QtCore" -I"D:/Qt/4.0.0/inc
lude" -I"." -I"D:/Qt/4.0.0/include/ActiveQt" -I"release" -I"." -I"d:\Qt\4.0.0\mk
specs\win32-g++" -o release\a.o a.cpp
a.cpp: In function `int qMain(int, char**)':
a.cpp:8: error: `tr' undeclared (first use this function)
a.cpp:8: error: (Each undeclared identifier is reported only once for each funct
ion it appears in.)
a.cpp:13:6: warning: no newline at end of file
mingw32-make: *** Error 1
mingw32-make: Leaving directory `D:/123'
mingw32-make: *** Error 2

D:\123>
我是在win下编译的,不是在linux下,如果在LINUX,一定可以出中文,这点我相信

zzh2004linux 发表于 2005-7-6 19:11:08

如果把中文换英文,编译运行一切ok

yunfan 发表于 2005-7-6 19:18:46

这样当然不行了,
把 tr 的地方换成 fromLocal8Bit , 然后试试看。

你最好把 #include <qstring.h> 给加上

sejishikong 发表于 2005-7-7 08:41:33

你刚开始学QT吧。
刚开始不用考虑中文显示的问题,都用英文,最后处理也行。
要用tr要#include <qtranslator.h>
要用fromLocal8bit应该#include <qtextcodec.h>,这个记不太清楚了。

sejishikong 发表于 2005-7-7 08:54:05

如果要做程序的国际化,最好使用tr方法,尤其是windows下。如果只想显示中文,也可以用tr方法,比如这样:
#include <qapplication.h>
#include <qlabel.h>
#include <qtextcodec.h>
int main(int argc,char *argv[])
{
   QApplicationapp(argc,argv);
   qApp->setDefaultCodec(QTextCodec::codecForName("GBK"));
   QLabellabel(tr("你好,世界!"),NULL);
   app.setMainWidget(&label);
   label.show();
   return app.exec();
}

zzh2004linux 发表于 2005-7-7 17:29:40

输入上面的代码,还是错误提示
make
mingw32-make -f Makefile.Release
mingw32-make: Entering directory `D:/123'
g++ -c -O2 -O2 -frtti -fexceptions -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_D
LL -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN
-I"D:/Qt/4.0.0/include/QtGui" -I"D:/Qt/4.0.0/include/QtCore" -I"D:/Qt/4.0.0/inc
lude" -I"." -I"D:/Qt/4.0.0/include/ActiveQt" -I"release" -I"." -I"d:\Qt\4.0.0\mk
specs\win32-g++" -o release\a.o a.cpp
a.cpp: In function `int qMain(int, char**)':
a.cpp:8: error: `tr' undeclared (first use this function)
a.cpp:8: error: (Each undeclared identifier is reported only once for each funct
ion it appears in.)
a.cpp:13:6: warning: no newline at end of file
mingw32-make: *** Error 1
mingw32-make: Leaving directory `D:/123'
mingw32-make: *** Error 2

zzh2004linux 发表于 2005-7-7 17:32:46

#include <QApplication>
#include <QPushButton>
#include <qstring.h>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QPushButton hello(fromLocal8Bit("你好"));
hello.resize(100, 30);

hello.show();
return app.exec();
}
这样也不行

zzh2004linux 发表于 2005-7-7 17:37:15

#include <QApplication>
#include <QPushButton>
#include <qstring.h>
#include <qtextcodec.h>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QPushButton hello(fromLocal8Bit("你好"));
hello.resize(100, 30);

hello.show();
return app.exec();
}
也不行

zzh2004linux 发表于 2005-7-7 17:41:07

我用的是qt4不是qt3,qt3的hello world都不能在qt4上编译

pupilzeng 发表于 2005-7-7 21:17:33

我不懂qt,但是我觉得你的这个问题,应该跟源代码的编码有关
建议不要在源代码中使用中文,全部使用英文,然后使用翻译的办法

KDE 发表于 2005-7-7 22:23:20

在源代码中使用中文是个恶习。

KanKer 发表于 2005-7-8 09:44:52

中文用trUtf8括起来,为国际化做准备。
页: [1] 2
查看完整版本: qt4 无法显示中文