I am not entirely sure about what you want to accomplish from the question. I think you are looking for a way to load the "qm" files at runtime from C++ code. If so, what you will need to do is use the QTranslator class..
QTranslator translator;
translator.load("file.qm", "dir");
qApp->installTranslator(&translator);
Update
This was working for me..
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
isChinese(false)
{
ui->setupUi(this);
msgbox = new QMessageBox(QMessageBox::NoIcon, "", "", QMessageBox::NoButton, this);
pbtn = msgbox->addButton("Ok", QMessageBox::AcceptRole);
doRetranslate();
QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(doTranslate()));
QObject::connect(ui->pushButton, SIGNAL(clicked()), msgbox, SLOT(show()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::doTranslate()
{
QTranslator translator;
if (!isChinese) {
translator.load("test_zh_TW.qm", "../TestTranslator");
qApp->installTranslator(&translator);
isChinese = true;
} else {
translator.load("", ""); // Default is english
qApp->installTranslator(&translator);
isChinese = false;
}
ui->retranslateUi(this);
doRetranslate();
}
void MainWindow::doRetranslate()
{
msgbox->setWindowTitle(QObject::tr("ERROR"));
msgbox->setText(QObject::tr("Invalid IP adress"));
pbtn->setText(QObject::tr("Ok"));
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMessageBox>
#include <QPushButton>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void doRetranslate();
public slots:
void doTranslate();
private:
Ui::MainWindow *ui;
bool isChinese;
QMessageBox *msgbox;
QPushButton *pbtn;
};
#endif // MAINWINDOW_H
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>100</x>
<y>70</y>
<width>181</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>Hello World!</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>140</x>
<y>120</y>
<width>93</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string>translate</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>
<receiver>label</receiver>
<slot>update()</slot>
<hints>
<hint type="sourcelabel">
<x>199</x>
<y>165</y>
</hint>
<hint type="destinationlabel">
<x>206</x>
<y>114</y>
</hint>
</hints>
</connection>
</connections>
</ui>