• 10876阅读
  • 10回复

[提问]换环境,汉字显示乱码,求教!( 新问题,欢迎站长进来) [复制链接]

上一主题 下一主题
离线fanbinnet
 

只看楼主 倒序阅读 楼主  发表于: 2007-08-16
— 本帖被 XChinux 从 Qt基础编程 移动到本区(2013-04-01) —
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));


addBtn = new QAction(QIcon(":/image/Resources/add.png"),tr("添加按钮(&B)"), this);
addBtn->setShortcut(tr("Ctrl+B"));
addBtn->setStatusTip(tr("添加一个新的按钮"));
connect(addBtn, SIGNAL(triggered()), this, SLOT(addButton()));

这段代码汉字显示在我机器上(XP)没有任何问题,但放到另一台 2003的机器就显示乱码了,请问这是什么原因。
[ 此贴被fanbinnet在2007-08-17 10:09重新编辑 ]
离线XChinux

只看该作者 1楼 发表于: 2007-08-16
将plugins/codecs/qcncodecs4.dll带上。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线fanbinnet

只看该作者 2楼 发表于: 2007-08-17
谢谢,站长 果然是这个原因,佩服呀。
离线fanbinnet

只看该作者 3楼 发表于: 2007-08-17
toolButton_1->setText(QApplication::translate("MainBarClass", "\346\233\262\347\272\277\346\230\276\347\244\272", 0, QApplication::UnicodeUTF8));

我是 用的VS2005,这是界面生成的代码,在没有qcncodecs4.dll的情况下也可以正常显示汉字。
我想问下,QT下是用什么函数把汉字的字符 翻译成类似这样的字符串("\346\233\262\347\272\277\346\230\276\347\244\272")
离线liur
只看该作者 4楼 发表于: 2007-08-17
x 确实强悍.

顶3楼的问题.也想知道!
离线XChinux

只看该作者 5楼 发表于: 2007-08-17
这个。。。你可能能看到许多网页上的汉字是正常显示的,但网页源面中却是用形如
ᅅᅅ
这样的形式表现的。(为了不被浏览器解释,所以将它写成全角的了)
反正就是Unicode编码。其它的俺也没研究过。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线jzj139
只看该作者 6楼 发表于: 2007-08-18
XChinux确实满厉害的  。。。你们不说大家好像都知道
qt
离线hesternal

只看该作者 7楼 发表于: 2007-08-20
为什么我的qt的plugin里没有qcncodecs4.dll,只有qcncodecsd.dll和qcncodecs.dll。把这两个都copy到exe目录下,到其他机器里去运行还是乱码。
离线hesternal

只看该作者 8楼 发表于: 2007-08-20
我发现即使在我的开发环境里,如果生成的exe和QtGui4.dll等必要的库放到同一个目录里,运行时,中文显示就是乱码,exe所在目录下没有dll,运行正常。版主知道这是怎么回事吗?
我的开发环境是vs2005和qt4.2.2。
离线hesternal

只看该作者 9楼 发表于: 2007-08-21
用codecForName(“GBK”)在exe和dll在同一个目录时获取不到正确的QTextCodec,换成codecForLocal()问题就解决了。为什么呢?
离线hanrai

只看该作者 10楼 发表于: 2007-09-02
我曾经问过Qt类似的问题,Qt官方的回答如下

Furthermore the wrong textrendering is not related to Windows CE at all. You
can recognize the same on an English Windows XP installation. As
fromLocal8Bit uses the local encoding, it will use the english one on an
english system. Thus, usage of Qt's internationalization is suggested. I
recommend to use English strings inside your source code and translate them
using the QObject::tr()/QObject::trUtf8(). For more information about our
internationalization system, continue reading at:
http://doc.trolltech.com/4.3/i18n.html
http://doc.trolltech.com/4.3/linguist-manual.html

In case you are sure you want to use Asian letters inside the source code
regardless the system to be run on, you can take a look at the source code
attached to this email. There it passes the character as raw data to a
QByteArray and encodes it manually by setting the codec for a QTextStream..
However, this is a very unconvenient solution and above described way is
much more recommended.

Best Regards,
Maurice Kalinowski

#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QTextCodec>
#include <QTextStream>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    app.setFont(QFont("SimSun", 18, QFont::Bold));

    QByteArray ba("いゅ");
    QTextStream s(&ba);
    s.setCodec("Big5");
    QString str;
    s >> str;
    QPushButton quit(str);

    quit.resize(75, 30);

    QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));

    quit.show();
    return app.exec();
}

不推荐直接在程序源代码中使用中文字符,推荐使用QT的国际化功能。直接使用中文可能会在不同的平台上产生不同的效果,这与程序使用的默认本地解码器有关。
而且tr本来就是支持国际化的工具,直接用国际化更能会非常完美的解决这些问题。
快速回复
限100 字节
 
上一个 下一个