#include <qapplication.h>
#include <qdialog.h>
#include <qlabel.h>
#include <qstring.h>
#include <qtextcodec.h>
//这里我给你两种方法,你自己看一下。用哪种方法你就把另一种给注掉
#define METHOD1
//#define METHOD2
#ifdef METHOD2
QTextCodec *gp_codec;
#endif
class TestDlg : public QDialog
{
public:
TestDlg(QWidget *parent = 0, const char *name = 0, bool modal = FALSE, WFlags f = 0);
~TestDlg();
};
TestDlg::TestDlg(QWidget *parent /* = 0 */, const char *name /* = 0 */, bool modal /* = FALSE */, WFlags f /* = 0 */) : QDialog(parent, name, modal, f)
{
QLabel *lbl_Title = new QLabel(this, "lbl_Title", 0);
QString str = "测试";
#if defined(METHOD1)
lbl_Title->setText(tr(str));
#elif defined(METHOD2)
lbl_Title->setText(gp_codec->toUnicode(str));
#endif
lbl_Title->setGeometry(10, 10, lbl_Title->sizeHint().width(), lbl_Title->sizeHint().height());
}
TestDlg::~TestDlg()
{
}
int main( int argc, char** argv )
{
QApplication app( argc, argv );
#if defined(METHOD1)
QTextCodec *pCodec = QTextCodec::codecForName("GB2312");
app.setDefaultCodec(pCodec);
#elif defined(METHOD2)
gp_codec = QTextCodec::codecForName("GB2312");
#endif
TestDlg dialog( 0, 0, TRUE );
app.setMainWidget(&dialog);
dialog.exec();
return 0;
}