PDA

View Full Version : Why the *.qm file only translates application partly?



FinderCheng
17th October 2009, 13:06
Hi, there! I have a strange problem about .qm file which generated by linguist. For example, I have a main window which extends QMainWindow and 2 dialogs which extends QDialog. Then I use lupdate and linguist to generate .qm file to translate my application. But when I run this application, only the main window is translated, the dailogs are not! I don't know why. Here is my code, I think there maybe are some problem with the namespace.



// mainwindow.cpp
#include <QAction>
#include <QToolBar>
#include "mainwindow.h"
#include "dialog1.h"
#include "dialog2.h"

app::MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QAction *openDialog1 = new QAction(tr("open dialog 1"), this);
QAction *openDialog2 = new QAction(tr("open dialog 2"), this);

connect(openDialog1, SIGNAL(triggered()), this, SLOT(showDialog1()));
connect(openDialog2, SIGNAL(triggered()), this, SLOT(showDialog2()));

QToolBar *bar = addToolBar(tr("Tool Bar"));
bar->addAction(openDialog1);
bar->addAction(openDialog2);
}

app::MainWindow::~MainWindow()
{

}

void app::MainWindow::showDialog1()
{
Dialog1 *d = new Dialog1(this);
d->exec();
}

void app::MainWindow::showDialog2()
{
Dialog2 *d = new Dialog2(this);
d->exec();
}




// dialog1.cpp
#include "dialog1.h"

app::Dialog1::Dialog1(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("Dialog 1"));
}

// Class Dialog2 is the same as Dialog1, so the file is ingored.




// main.cpp
#include <QtGui/QApplication>
#include <QDebug>
#include <QTranslator>
#include "mainwindow.h"

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

QApplication a(argc, argv);

QTranslator t;
qDebug() << t.load("../res/i18n_zh_CN");
a.installTranslator(&t);

MainWindow w;
w.show();
return a.exec();
}


qDebug() outputs true. This has confused for several days....

FinderCheng
17th October 2009, 15:04
OK, I found the reason! Because tr() must with Q_OBJECT! I added this macro and it works!