File tree 9 files changed +492
-2
lines changed
9 files changed +492
-2
lines changed Original file line number Diff line number Diff line change 1
1
#include " mainwindow.h"
2
2
#include < QApplication>
3
3
#include < QTranslator>
4
+ #include < QLocale>
5
+ #include < QDebug>
4
6
5
7
6
8
/*
12
14
* 3. 函数中指定翻译文件
13
15
* 如下主文件中所示
14
16
* 需要注意的是,翻译需要依赖提取tr中的文本内容,因此需要翻译的地方需要使用tr处理对应的文本
15
- *
17
+ * 当然以上步骤也能经过 工具->外部->Qt语言专家->(lupdate/lrelease)来完成
16
18
*/
17
19
18
20
@@ -23,7 +25,9 @@ int main(int argc, char *argv[])
23
25
{
24
26
QApplication a (argc, argv);
25
27
26
-
28
+ // 如果想做成自动根据当地语言环境来设置翻译文件,可以使用 QLocale::system().name() 来获取
29
+ // 这样就能根据当地语言来加载不同的翻译qm文件了
30
+ // qDebug() << QLocale::system().name(); // "zh_CN"
27
31
QTranslator translator;
28
32
translator.load (" ../qt_I18N/tra.qm" );
29
33
a.installTranslator (&translator);
Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" utf-8" ?>
2
+ <!DOCTYPE TS >
3
+ <TS version =" 2.1" language =" zh_CN" >
4
+ <context >
5
+ <name >MainWindow</name >
6
+ <message >
7
+ <location filename =" mainwindow.ui" line =" 14" />
8
+ <source >MainWindow</source >
9
+ <translation >主窗口</translation >
10
+ </message >
11
+ <message >
12
+ <location filename =" mainwindow.ui" line =" 27" />
13
+ <source >PushButton</source >
14
+ <translation >按钮</translation >
15
+ </message >
16
+ <message >
17
+ <location filename =" mainwindow.ui" line =" 42" />
18
+ <source >File</source >
19
+ <translation >文件</translation >
20
+ </message >
21
+ <message >
22
+ <location filename =" mainwindow.ui" line =" 59" />
23
+ <source >New</source >
24
+ <translation >新建</translation >
25
+ </message >
26
+ <message >
27
+ <location filename =" mainwindow.cpp" line =" 12" />
28
+ <source >hello Qt!</source >
29
+ <translation >你好QT!</translation >
30
+ </message >
31
+ <message >
32
+ <location filename =" mainwindow.cpp" line =" 16" />
33
+ <source >password</source >
34
+ <comment >mainwindow</comment >
35
+ <translation >密码</translation >
36
+ </message >
37
+ <message >
38
+ <location filename =" mainwindow.cpp" line =" 22" />
39
+ <source >ID is %1,Name is %2</source >
40
+ <translation >身份证: %1,名字: %2</translation >
41
+ </message >
42
+ </context >
43
+ </TS >
Original file line number Diff line number Diff line change
1
+ #include " mainwindow.h"
2
+ #include < QApplication>
3
+
4
+ int main (int argc, char *argv[])
5
+ {
6
+ QApplication a (argc, argv);
7
+ MainWindow w;
8
+ w.show ();
9
+
10
+ return a.exec ();
11
+ }
Original file line number Diff line number Diff line change
1
+ #include " mainwindow.h"
2
+ #include " ui_mainwindow.h"
3
+
4
+ MainWindow::MainWindow (QWidget *parent) :
5
+ QMainWindow(parent),
6
+ ui(new Ui::MainWindow)
7
+ {
8
+ ui->setupUi (this );
9
+ }
10
+
11
+ MainWindow::~MainWindow ()
12
+ {
13
+ delete ui;
14
+ }
Original file line number Diff line number Diff line change
1
+ #ifndef MAINWINDOW_H
2
+ #define MAINWINDOW_H
3
+
4
+ #include < QMainWindow>
5
+
6
+ namespace Ui {
7
+ class MainWindow ;
8
+ }
9
+
10
+ class MainWindow : public QMainWindow
11
+ {
12
+ Q_OBJECT
13
+
14
+ public:
15
+ explicit MainWindow (QWidget *parent = nullptr );
16
+ ~MainWindow ();
17
+
18
+ private:
19
+ Ui::MainWindow *ui;
20
+ };
21
+
22
+ #endif // MAINWINDOW_H
Original file line number Diff line number Diff line change
1
+ <ui version =" 4.0" >
2
+ <class >MainWindow</class >
3
+ <widget class =" QMainWindow" name =" MainWindow" >
4
+ <property name =" geometry" >
5
+ <rect >
6
+ <x >0</x >
7
+ <y >0</y >
8
+ <width >400</width >
9
+ <height >300</height >
10
+ </rect >
11
+ </property >
12
+ <property name =" windowTitle" >
13
+ <string >MainWindow</string >
14
+ </property >
15
+ <widget class =" QMenuBar" name =" menuBar" />
16
+ <widget class =" QToolBar" name =" mainToolBar" />
17
+ <widget class =" QWidget" name =" centralWidget" />
18
+ <widget class =" QStatusBar" name =" statusBar" />
19
+ </widget >
20
+ <layoutDefault spacing =" 6" margin =" 11" />
21
+ <pixmapfunction ></pixmapfunction >
22
+ <resources />
23
+ <connections />
24
+ </ui >
Original file line number Diff line number Diff line change
1
+ # -------------------------------------------------
2
+ #
3
+ # Project created by QtCreator 2023-09-29T11:59:34
4
+ #
5
+ # -------------------------------------------------
6
+
7
+ QT += core gui
8
+
9
+ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10
+
11
+ TARGET = qt_whatisit
12
+ TEMPLATE = app
13
+
14
+ # The following define makes your compiler emit warnings if you use
15
+ # any feature of Qt which has been marked as deprecated (the exact warnings
16
+ # depend on your compiler). Please consult the documentation of the
17
+ # deprecated API in order to know how to port your code away from it.
18
+ DEFINES += QT_DEPRECATED_WARNINGS
19
+
20
+ # You can also make your code fail to compile if you use deprecated APIs.
21
+ # In order to do so, uncomment the following line.
22
+ # You can also select to disable deprecated APIs only up to a certain version of Qt.
23
+ # DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
24
+
25
+ CONFIG += c++11
26
+
27
+ SOURCES += \
28
+ main.cpp \
29
+ mainwindow.cpp
30
+
31
+ HEADERS += \
32
+ mainwindow.h
33
+
34
+ FORMS += \
35
+ mainwindow.ui
36
+
37
+ # Default rules for deployment.
38
+ qnx: target.path = /tmp/ $${TARGET }/bin
39
+ else: unix :!android: target.path = /opt/ $${TARGET }/bin
40
+ !isEmpty (target.path ): INSTALLS += target
You can’t perform that action at this time.
0 commit comments