-
Notifications
You must be signed in to change notification settings - Fork 2
/
mainwindow.cpp
80 lines (64 loc) · 2.16 KB
/
mainwindow.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QTranslator>
#include <QDebug>
#include "utils.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
, languageMap(QMap<int, QString>())
, customLabel(QNUMERIC_H)
{
ui->setupUi(this);
// init language map
languageMap.insert(0, QLocale::system().name());
languageMap.insert(1, "en_US");
languageMap.insert(2, "zh_cn");
customLabel = new QLabel(this);
ui->verticalLayout->addWidget(customLabel);
QString currentLocalName = Utils::getLocalName();
if (languageMap.values().contains(currentLocalName)) {
ui->comboBox->setCurrentIndex(languageMap.key(currentLocalName));
}
connect(ui->comboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(onLanguageSelectChanged(int)));
retranslateCustomUi();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *event)
{
if (event->type() == QEvent::LanguageChange) {
// retranslateUi contains clear comboBox, so disconnect first.
disconnect(ui->comboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(onLanguageSelectChanged(int)));
ui->retranslateUi(this);
connect(ui->comboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(onLanguageSelectChanged(int)));
ui->label->setText(tr("Hello,Qt."));
retranslateCustomUi();
}
QMainWindow::changeEvent(event);
}
void MainWindow::onLanguageSelectChanged(int index)
{
if (languageMap.size() < index + 1)
return;
bool isEffectImmediately = ui->checkBox->isChecked();
if (!isEffectImmediately) {
QMessageBox::information(this, tr("Hint"), tr("Effect when restart the app."));
} else {
// TODO: should install one times.
QTranslator *translator = new QTranslator(this);
translator->load(Utils::findQmFile(languageMap.value(index)));
qApp->installTranslator(translator);
}
Utils::setLocalName(languageMap.value(index));
}
void MainWindow::retranslateCustomUi()
{
customLabel->setText(tr("I'm custom label, not in designer."));
}