加载qm时,有些没有翻译过来,显示的还是原本的! 是这样的,我想把所有成员变量写在一个类中,这些成员变量是公共 的,供其它类来访问修改,那么问题来了:在其它类中调用全局变量或全局类后翻译后,加载时没有翻译过来,都保持了原样; 网上说的大部分资料我都看过,大部分针对的是乱码问题,对于我这种形式的介绍的比较少,我不知道怎么办,所以来求助各位大侠:
我的问题就是:全局的类实例变量, 在其它类中访问类成员的时候,用Qt Linguist 翻译后没有加载翻译过来![color=rgba(0, 0, 0, 0)]
代码抽出来了,简化成以下示例:test.pro:[color=rgba(0, 0, 0, 0)]
- QT += core gui
- TARGET = Test
- TEMPLATE = app
- SOURCES += main.cpp test.cpp global.cpp
- HEADERS += test.h global.h
- FORMS += test.ui
- TRANSLATIONS = test.ts
- RESOURCES += test.qrc
全局类:
global.h:[color=rgba(0, 0, 0, 0)]
- #ifndef GLOBAL_H
- #define GLOBAL_H
- #include <QString>
- class Global
- {
- public:
- Global();
- QString strTest;
- };
- **global.cpp:**
- extern Global myGlobal;
- #endif // GLOBAL_H
- #include "global.h"
- #include <QObject>
- Global myGlobal;
- Global::Global()
- {
- strTest = QObject::tr("Text");
- };
测试类:
test.h:[color=rgba(0, 0, 0, 0)]
- #ifndef TEST_H
- #define TEST_H
- #include <QDialog>
- namespace Ui {
- class Test;
- }
- class Test : public QDialog
- {
- Q_OBJECT
- public:
- explicit Test(QWidget *parent = 0);
- ~Test();
- private:
- Ui::Test *ui;
- };
- #endif // TEST_H
test.cpp:[color=rgba(0, 0, 0, 0)]
- #include "test.h"
- #include "ui_test.h"
- #include "global.h"
- Test::Test(QWidget *parent) :
- QDialog(parent),
- ui(new Ui::Test)
- {
- ui->setupUi(this);
- //文字还是翻译前的,没有进行更新:
- ui->lbTest->setText(myGlobal.strTest);
- //如果定义一个局部变量,是可以成功显示翻译后的文本,但是这样没用呀,因为这个类变量要是全局的,其它类也要访问和修改:
- //Global test;
- //ui->lbTest->setText(test.strTest);
- }
- Test::~Test()
- {
- delete ui;
- }
test.ui:就加一个标签控件,用于显示文本[color=rgba(0, 0, 0, 0)]
- <?xml version="1.0" encoding="UTF-8"?>
- <ui version="4.0">
- <class>Test</class>
- <widget class="QDialog" name="Test">
- <property name="geometry">
- <rect>
- <x>0</x>
- <y>0</y>
- <width>400</width>
- <height>300</height>
- </rect>
- </property>
- <property name="windowTitle">
- <string>Test</string>
- </property>
- <layout class="QGridLayout" name="gridLayout">
- <item row="0" column="0">
- <widget class="QLabel" name="lbTest">
- <property name="text">
- <string/>
- </property>
- <property name="alignment">
- <set>Qt::AlignCenter</set>
- </property>
- </widget>
- </item>
- </layout>
- </widget>
- <layoutdefault spacing="6" margin="11"/>
- <resources/>
- <connections/>
- </ui>
Main.cpp:[color=rgba(0, 0, 0, 0)]
- #include <QtGui/QApplication>
- #include "test.h"
- #include <QTextCodec>
- #include <QTranslator>
- QTranslator *myTranslator;
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- myTranslator = new QTranslator();
- myTranslator->load(":/test.qm");
- a.installTranslator(myTranslator);
- Test w;
- w.show();
- return a.exec();
- }
用Qt Linguist 将文本“Text”翻译成“Update Text”:
编译运行,没有变化,显示的还是原“Text”!
如果在test类中定义局部变量,就会成功,显示的是翻译后的(但是局部的没用,不是我想要的,我需要的是全局的):
期间:
文件编码用GB2312和UTF-8都试过,翻译都无效!
tr编码用GB2312和UTF-8都试过,翻译也都无效!
头疼呀 ,请各位大侠赐教,谢谢!