Skip to content

Commit 81f98bf

Browse files
添加对翻译器的使用说明
1 parent 177e758 commit 81f98bf

File tree

9 files changed

+250
-0
lines changed

9 files changed

+250
-0
lines changed

qt_I18N/main.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "mainwindow.h"
2+
#include <QApplication>
3+
#include <QTranslator>
4+
5+
6+
/*
7+
* 执行文件翻译的过程
8+
* 1. 使用lupdate工具根据 pro文件获得ts文件 lupdate -pro qt_I18N.pro -ts tra.ts
9+
* lupdate -pro myI18N.pro
10+
* 2. 使用lrelease工具根据ts文件获得qm文件 lrelease [options] ts-files [-qm qm-file]
11+
* lrelease myI18N_zh_CN.ts
12+
* 3. 函数中指定翻译文件
13+
* 如下主文件中所示
14+
* 需要注意的是,翻译需要依赖提取tr中的文本内容,因此需要翻译的地方需要使用tr处理对应的文本
15+
*
16+
*/
17+
18+
19+
20+
21+
22+
int main(int argc, char *argv[])
23+
{
24+
QApplication a(argc, argv);
25+
26+
27+
QTranslator translator;
28+
translator.load("../qt_I18N/tra.qm");
29+
a.installTranslator(&translator);
30+
31+
MainWindow w;
32+
w.show();
33+
return a.exec();
34+
}

qt_I18N/mainwindow.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "mainwindow.h"
2+
#include "ui_mainwindow.h"
3+
#include <QLabel>
4+
5+
MainWindow::MainWindow(QWidget *parent) :
6+
QMainWindow(parent),
7+
ui(new Ui::MainWindow)
8+
{
9+
ui->setupUi(this);
10+
11+
QLabel *label = new QLabel(this);
12+
label->setText(tr("hello Qt!"));
13+
label->move(100,50);
14+
15+
QLabel *label2 = new QLabel(this);
16+
label2->setText(tr("password", "mainwindow"));
17+
label2->move(100,80);
18+
19+
QLabel *label3 = new QLabel(this);
20+
int id = 123;
21+
QString name = "yafei";
22+
label3->setText(tr("ID is %1,Name is %2").arg(id).arg(name));
23+
label3->resize(150, 12);
24+
label3->move(100, 120);
25+
}
26+
27+
MainWindow::~MainWindow()
28+
{
29+
delete ui;
30+
}

qt_I18N/mainwindow.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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

qt_I18N/mainwindow.ui

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>MainWindow</class>
4+
<widget class="QMainWindow" name="MainWindow">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>581</width>
10+
<height>433</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>MainWindow</string>
15+
</property>
16+
<widget class="QWidget" name="centralWidget">
17+
<widget class="QPushButton" name="pushButton">
18+
<property name="geometry">
19+
<rect>
20+
<x>110</x>
21+
<y>260</y>
22+
<width>89</width>
23+
<height>25</height>
24+
</rect>
25+
</property>
26+
<property name="text">
27+
<string>PushButton</string>
28+
</property>
29+
</widget>
30+
</widget>
31+
<widget class="QMenuBar" name="menuBar">
32+
<property name="geometry">
33+
<rect>
34+
<x>0</x>
35+
<y>0</y>
36+
<width>581</width>
37+
<height>28</height>
38+
</rect>
39+
</property>
40+
<widget class="QMenu" name="menu">
41+
<property name="title">
42+
<string>File</string>
43+
</property>
44+
<addaction name="actionNew"/>
45+
</widget>
46+
<addaction name="menu"/>
47+
</widget>
48+
<widget class="QToolBar" name="mainToolBar">
49+
<attribute name="toolBarArea">
50+
<enum>TopToolBarArea</enum>
51+
</attribute>
52+
<attribute name="toolBarBreak">
53+
<bool>false</bool>
54+
</attribute>
55+
</widget>
56+
<widget class="QStatusBar" name="statusBar"/>
57+
<action name="actionNew">
58+
<property name="text">
59+
<string>New</string>
60+
</property>
61+
</action>
62+
</widget>
63+
<layoutdefault spacing="6" margin="11"/>
64+
<resources/>
65+
<connections/>
66+
</ui>

qt_I18N/qt_I18N.pro

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#-------------------------------------------------
2+
#
3+
# Project created by QtCreator 2023-09-29T11:06:42
4+
#
5+
#-------------------------------------------------
6+
7+
QT += core gui
8+
9+
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10+
11+
TARGET = qt_I18N
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

qt_colorbutton/mainwindow.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,42 @@
11
#include "mainwindow.h"
22
#include "ui_mainwindow.h"
33

4+
#include <QFile>
5+
6+
47
MainWindow::MainWindow(QWidget *parent) :
58
QMainWindow(parent),
69
ui(new Ui::MainWindow)
710
{
811
ui->setupUi(this);
12+
// // 当然除了手动去ui中进行设置,也可以直接加载qss文件来实现设置
13+
// auto qssFile = new QFile(":/css/my.qss", this);
14+
// qssFile->open(QFile::ReadOnly);
15+
16+
// QString styleSheet = tr(qssFile->readAll());
17+
// qApp->setStyleSheet(styleSheet);
18+
// qssFile->close();
19+
20+
// // 设置窗口的不透明度为0.5
21+
// setWindowOpacity(0.7);
22+
// FramelessWindowHint 实现窗体透明效果
23+
// setWindowFlags(Qt::FramelessWindowHint);
24+
// 使窗体透明但是其他部件不受影响
25+
setAttribute(Qt::WA_TranslucentBackground);
26+
27+
// // 创建阴影效果
28+
// QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
29+
// // 设置阴影颜色
30+
// effect->setColor(QColor(100, 100, 100, 100));
31+
// // 设置阴影模糊半径
32+
// effect->setBlurRadius(2);
33+
// // 设置阴影偏移值
34+
// effect->setOffset(10);
35+
// // 标签部件使用阴影效果
36+
// ui->label->setGraphicsEffect(effect);
37+
38+
39+
940
}
1041

1142
MainWindow::~MainWindow()

qt_colorbutton/mainwindow.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class MainWindow : public QMainWindow
1515
explicit MainWindow(QWidget *parent = nullptr);
1616
~MainWindow();
1717

18+
void on_pushButton_clicked();
19+
1820
private:
1921
Ui::MainWindow *ui;
2022
};

qt_colorbutton/mainwindow.ui

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,28 @@ QPushButton:pressed {
9090
<bool>false</bool>
9191
</attribute>
9292
</widget>
93+
<widget class="QToolBar" name="toolBar_2">
94+
<property name="windowTitle">
95+
<string>toolBar_2</string>
96+
</property>
97+
<attribute name="toolBarArea">
98+
<enum>TopToolBarArea</enum>
99+
</attribute>
100+
<attribute name="toolBarBreak">
101+
<bool>false</bool>
102+
</attribute>
103+
</widget>
104+
<widget class="QToolBar" name="toolBar_3">
105+
<property name="windowTitle">
106+
<string>toolBar_3</string>
107+
</property>
108+
<attribute name="toolBarArea">
109+
<enum>TopToolBarArea</enum>
110+
</attribute>
111+
<attribute name="toolBarBreak">
112+
<bool>false</bool>
113+
</attribute>
114+
</widget>
93115
</widget>
94116
<layoutdefault spacing="6" margin="11"/>
95117
<resources/>

qt_colorbutton/res.qrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
<qresource prefix="/images">
33
<file alias="achou">achou.png</file>
44
</qresource>
5+
<qresource prefix="/css">
6+
<file>my.qss</file>
7+
</qresource>
58
</RCC>

0 commit comments

Comments
 (0)