Bonjour, j'ai créer le programme suivant :
Dans le QComboBox, il y a deux choix possible : Français ou Anglais. Selon le choix de l'utilisateur, je traduis cette application sans la redémarrer, tout se fait pendant l'exécution. Dès que l'utilisateur enfonce le QPushButton Traduire un slot translate() est appelé. Voici le code source :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 //main.cpp #include "window.hpp" int main(int argc, char* argv[]) { QApplication app(argc, argv); Window myWindow; return app.exec(); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 //window.hpp #ifndef WINDOW__HPP #define WINDOW__HPP #include <QtGlobal> #include <QtCore> #include <QtGui> class Window : public QWidget { Q_OBJECT private: QComboBox* m_comboBox; QPushButton* m_pushButton; QTranslator* m_translator; Q_SLOT void translate(); public: explicit Window(QWidget* widgetParent = 0); }; #endif // WINDOW__HPPA l'exécution rien ne se passe j'ai tout essayer QCoreApplication::removeTranslator(), etc... Notez que je ne veux pas passer par Qt Designer.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 window.cpp #include "window.hpp" void Window::translate() { if (m_comboBox->currentText() == "Français") { m_translator->load("test_trad_fr"); } else { m_translator->load("test_trad_en"); } } Window::Window(QWidget* widgetParent) : QWidget(widgetParent) { m_comboBox = new QComboBox(this); m_comboBox->addItem(QObject::tr("Français")); m_comboBox->addItem(QObject::tr("Anglais")); m_pushButton = new QPushButton(QObject::tr("Traduire"), this); QObject::connect(m_pushButton, SIGNAL(released()), this, SLOT(translate())); QHBoxLayout* myLayout = new QHBoxLayout(this); myLayout->addWidget(m_comboBox); myLayout->addWidget(m_pushButton); this->setLayout(myLayout); m_translator = new QTranslator(this); m_translator->load("test_trad" + QLocale::system().name().section('_', 0, 0)); qApp->installTranslator(m_translator); this->setWindowTitle(QObject::tr("Test de traduction")); this->setFixedSize(300, 0); this->show(); }
A côté de l'exécutable j'ai un fichier test_trad_en.qm, mais pas de test_trad_fr.qm, puisqu'il est déjà en dur dans le code source.
Merci pour vos réponses.
Partager