前言
Qt默认生成的可执行文件是没有应用的信息的:
有时候想给应用加点信息。
添加信息的方法
直接在.pro文件里加如下配置:
VERSION = 1.2.3.4 #这个是产品版本
QMAKE_TARGET_PRODUCT = 产品名称
QMAKE_TARGET_COMPANY = 公司
QMAKE_TARGET_DESCRIPTION = 文件说明
QMAKE_TARGET_COPYRIGHT = 版权
添加了之后效果如下:
显然出现了中文乱码。
解决中文乱码的方法
产生乱码的根本原因是:.pro文件的默认编码是UTF-8,而中国的windows系统默认系统编码是GBK编码。换句话说,在linux用Qt写应用信息默认编码下是不会出现中文乱码的。所以要处理中文乱码的情况一般只会出现在windows系统下。
方法一、不用中文
VERSION = 1.2.3.4 #这个是产品版本
QMAKE_TARGET_PRODUCT = PRODUCT
QMAKE_TARGET_COMPANY = COMPANY
QMAKE_TARGET_DESCRIPTION = DESCRIPTION
QMAKE_TARGET_COPYRIGHT = COPYRIGHT
效果:
可以看到英文是肯定没有乱码的。
方法二、修改.pro文件为GBK编码
首先用记事本.pro文件,把.pro文件另存为GBK编码(ANSI):
之后你的Qt打开这个.pro就会有中文乱码了:
然后点右上角的Select Encoding,把编码改成GBK:
按编码重新载入后就是正常的了:
然后再编译一次,中文乱码就没有了:
方法三、修改系统编码为UTF-8编码
这种方法不推荐!!!很容易出现以前配置的环境用不了了的情况。
方法四、用转义的方式来写中文
解决方式
推荐这种方法,不需要改任何编码,就可以解决乱码问题。
先上例子:
VERSION = 1.2.3.4 #这个是产品版本
win32 {
#windows用的是GBK编码,当前的.pro文件用的是UTF-8编码,所以要用转义字符才不会出现乱码
QMAKE_TARGET_PRODUCT = \xb2\xfa\xc6\xb7\xc3\xfb\xb3\xc6
QMAKE_TARGET_COMPANY = \xb9\xab\xcb\xbe
QMAKE_TARGET_DESCRIPTION = \xce\xc4\xbc\xfe\xcb\xb5\xc3\xf7
QMAKE_TARGET_COPYRIGHT = \xb0\xe6\xc8\xa8
} else {
#除了windows,其他系统基本就是用的UTF-8编码,所以直接打中文就可以了
QMAKE_TARGET_PRODUCT = 产品名称
QMAKE_TARGET_COMPANY = 公司
QMAKE_TARGET_DESCRIPTION = 文件说明
QMAKE_TARGET_COPYRIGHT = 版权
}
截图下看到是这样的:
效果:
我解释一下我写的配置是什么鬼。。。
每个字符都有对应的编码值,在GBK编码下,一个中文占两个字节。\xb2的意思就是十六进制的b2,也就是十进制的178;同理\xfa就是十六进制的fa,也就是十进制的250。结合我上面截图的例子,\xb2\xfa就是两个unsigned char,一个是unsigned char a = 178; 一个是unsigned char b = 250; 然后把a和b拼在一起,用GBK来编码输出,就正好是"产"这个字。
有图有真相,我向来以理服人:
解释:我是用windows的TDM-GCC编译器编译的,默认编码就是GBK,所以没有显式写用GBK编码。
用十六进制也是可以的:
然后C/C++也可以用转义的形式设置char的值,比如:
甚至直接这么设置数组都可以:
这就解释了我上面的.pro文件写的是什么鬼了。。。
获取编码值
这样就出现了一个问题:我怎么知道我要输入的中文的编码值是多少,从编码值恢复回中文容易(直接cout不就出来了吗),但是给一个中文要它的编码值就不是这么简单的了。
我直接用Qt写了个转义工具:
点转义后:
也可以还原:
这个工具的源码如下:
charset_tool.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = charset_tool
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_outputButton_clicked();
void on_humanButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextCodec>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_outputButton_clicked()
{
QString input = ui->inputLineEdit->text();
QString charset = ui->charsetLineEdit->text();
QTextCodec *codec = QTextCodec::codecForName(charset.toUtf8());
QByteArray byteArray = codec->fromUnicode(input);
int len = byteArray.size();
QString outputString = "";
for(int i=0; i<len; ++i)
{
unsigned char a = byteArray[i];
outputString.append("\\x"+QString::number(a, 16));
}
ui->outputLineEdit->setText(outputString);
}
void MainWindow::on_humanButton_clicked()
{
QString input = ui->inputLineEdit->text();
QString charset = ui->charsetLineEdit->text();
QTextCodec *codec = QTextCodec::codecForName(charset.toUtf8());
QStringList inputs = input.mid(1).split("\\");
QByteArray byteArray;
foreach(QString i, inputs)
{
if(i.length())
{
unsigned char a;
if(i[0]=='x' || i[0]=='X')
{
a = i.mid(1).toInt(0, 16);
}
else
{
a = i.toInt(0, 16);
}
byteArray.append(a);
}
else
{
ui->outputLineEdit->setText("输入有问题");
return ;
}
}
QString output = codec->toUnicode(byteArray);
ui->outputLineEdit->setText(output);
}
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>724</width>
<height>185</height>
</rect>
</property>
<property name="windowTitle">
<string>字符串转义以及还原工具</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0" colspan="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>599</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>输入:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>编码:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="charsetLineEdit"/>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="inputLineEdit"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>输出:</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="outputButton">
<property name="text">
<string>转义</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="outputLineEdit"/>
</item>
<item row="2" column="3">
<widget class="QPushButton" name="humanButton">
<property name="text">
<string>还原</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>724</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
如果懒得自己整这些代码,可以直接在github下载这个工具:
https://github.com/Eyre-Turing/charset_tool