当前位置:编程学习 > wap >>

Qt 国际化多语言时,mainwindow类翻译成功,但其他类没变化,求解

Qt 国际化多语言时,mainwindow类翻译成功,但其他类没变化,求解。
比如: QProgressDialog process(this);
    process.setLabelText(tr("processing..."));
中进度条窗口依然是英文显示"processing..."。
说明一点:我是先加载qm文件再调用其他类的
    QTranslator translator;
    bool b = false;
    b = translator.load("chinese.qm");
    a.installTranslator(&translator);
    .......
   QProgressDialog process(this);
    process.setLabelText(tr("processing..."));
国际化 --------------------编程问答-------------------- ts文件里有“processing...”? --------------------编程问答--------------------
引用 1 楼 jdwx1 的回复:
ts文件里有“processing...”?
有,所有用tr标注的字符都有,但加载.qm文件后除了mainwindow类被翻译外,其他类的均未翻译,求解 --------------------编程问答-------------------- 仔细检查一下,是不是有多个qm文件,程序加载的不是最新的qm。 --------------------编程问答-------------------- 是不是你修改后翻译文件没有重新编译,导致里面还是已欠费翻译的 --------------------编程问答-------------------- 这种问题经常出,qm、dll这些编译出新的忘了替换旧的,还是用的旧的文件,导致出现奇怪的问题。 --------------------编程问答-------------------- 应该是QTranslator translator;的作用域问题。我把QTranslator translator;定义为MainWindow的成员变量后,在MainWindow的成员函数里创建QProgressDialog,可以成功翻译了,但创建自定义的线程类时没有翻译。求解。代码大概如下:
//MainWindow.h
class ScanThread;
class MainWindow : public QMainWindow
{
    Q_OBJECT  
public:
    ......
    ScanThread *m_pThreadScan; //线程指针
    ......
}

//MainWindow.cpp
void MainWindow::ClickedOperationD() // 解密按钮函数
{
    ......
    QProgressDialog process(this);
    process.setLabelText(tr("processing..."));
    int max = myList.count()/6;
    process.setRange(0, max);
    process.setModal(true);
    process.setCancelButtonText(tr("cancel"));
    process.show();
    ......
}
void MainWindow::on_pBtn_scan_clicked()                   // 扫描按钮函数
{
    ......
    m_pThreadScan = new ScanThread(strPath, strFileType, this);
    m_pThreadScan->start();
}

自定义线程类
//QMyThread.h
class MainWindow;
class ScanThread : public QThread
{
public:
     ......
     MainWindow* m_pMainW;

    ScanThread(const QString path, const QString strFileType, MainWindow* MainW);
    bool FindFile(const QString path, const QString strFileType, int *j);
    virtual void run();
    ~ScanThread();
};

//QMyThread.cpp
void ScanThread::run()
{
    FindFile(m_path,m_strFileType,&m_j);
    QString str = tr("Finished scanning. Total number of files:");
    m_pMainW->m_pBText->setText(str + QString::number(m_j));
    ......
    QString strIfE = tr("Unencrypted");
    m_pMainW->ui->tableWidget->setItem(j, 2, new QTableWidgetItem(strIfE));
    ......
} --------------------编程问答-------------------- translator_->load(lang.lang_file, qApp->applicationDirPath() + "/")) {
            qApp->installTranslator(translator_); --------------------编程问答--------------------  process.setLabelText(tr("processing..."));
这一句改为这个试一下process.setLabelText(QObject::tr("processing..."));
补充:移动开发 ,  Qt
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,