在Qt中,如何全局设置setCodecForCStrings(在标头中)? [英] In Qt, how to setCodecForCStrings globally (in header)?

查看:1210
本文介绍了在Qt中,如何全局设置setCodecForCStrings(在标头中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C ++开发一堆Qt应用程序,它们都使用一些模块(翻译单元)来实现同样使用Qt的常见功能.

I'm developing a bunch of Qt applications in C++ and they all use some modules (translation units) for common functionality that use Qt as well.

每当我将C字符串(隐式转换)或C ++字符串对象(fromStdString())转换为QString对象时,我都希望原始数据采用UTF-8编码,反之亦然(toStdString()).

Whenever I convert a C string (implicit conversion) or C++ string object (fromStdString()) to a QString object, I expect the original data to be UTF-8 encoded and vice versa (toStdString()).

由于默认值为Latin-1 ,将编解码器手动"(在我的每个程序的init过程中)设置为UTF-8:

Since the default is Latin-1, I have to set the codec "manually" (in the init procedure of every one of my programs) to UTF-8:

QTextCodec :: setCodecForCStrings(QTextCodec :: codecForName("utf8"));

QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));

不是我的所有模块都具有初始化过程.包含类的模块可以执行此操作(我可以将此行放入类构造函数中),但是某些模块仅包含具有许多功能的名称空间.因此没有setCodecForCStrings()的地方.每当我隐式地(从我的一个模块中)从QString转换为QString时,我都依赖于主程序的init过程已经设置的编解码器,这似乎是一个很糟糕的解决方案.

Not all of my modules have an init procedure. The ones containing a class do (I can put this line in the class constructor), but some modules just contain a namespace with lots of functions. So there's no place for setCodecForCStrings(). Whenever I convert from/to a QString implicitly (from within one of my modules), I rely on the codec being already set by the init procedure of the main program, which seems to be a rather bad solution.

是否存在一种可靠的方法来将模块中的编解码器设置为UTF-8 ,或者我只需要非常小心,不要完全不使用隐式转换(至少在我的模块中)并编写类似std :: string(q.toUtf8().constData())而不是q.toStdString()吗?

Is there a reliable way to set the codec to UTF-8 in my modules, or will I just have to be very careful not to use implicit conversions at all (in my modules at least) and write something like std::string(q.toUtf8().constData()) instead of q.toStdString()?

推荐答案

这可以通过为自动实例化的单例相似类在其构造函数中包含一些初始化代码的类定义来完成:

This can be done using a class definition for an automatically instantiated singleton-similar class having some init code in its constructor:

class ModuleInit {
    static ModuleInit *instance;
public:
    ModuleInit() {
        QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
    }
};
ModuleInit * ModuleInit::instance = new ModuleInit(); // put this line in .cpp!

将此代码 anywhere 放到 any 项目中,会将文本编解码器设置为UTF-8.但是,您可以在main()方法中覆盖文本编解码器,因为即使在之前 main()方法也执行了上面的代码.

Putting this code anywhere into any project will set the text codec to UTF-8. You can, however, overwrite the text codec in the main() method, since the code above is executed even before the main() method.

对于" anywhere ",我当然是指在语法上允许使用的地方.将类定义放在某些头文件中,并将实例化放在相应的.cpp文件中.

With "anywhere" I of course mean places where it is syntactically allowed to. Put the class definition in some header file, and the instantiation in the appropriate .cpp file.

您甚至可以将整个代码放在一个.cpp文件中,比如说moduleinit.cpp您可以对其进行编译和链接,但绝不能明确地使用(没有头文件可以包含...) .除了第一个实例之外,这还将避免意外的实例化,并且类名重复不会有任何问题.

You can even put the whole code in one .cpp file, say moduleinit.cpp which you compile and link, but never explicitly use (there is no header file you could include...). This will also avoid accidental instantiations except the very first one and you will not have any problems with duplicate class names.

请注意,您不能为一个特定文件中的C字符串设置编解码器.使用QTextCodec::setCodecForCString设置编解码器将在整个应用程序范围内进行设置!

Note that you can't set the codec for C-strings in one particular file. Setting the codec using QTextCodec::setCodecForCString will set it application-wide!

这篇关于在Qt中,如何全局设置setCodecForCStrings(在标头中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆