QTextCodec子类 - 如何注册我的编解码器 [英] QTextCodec subclass - how to register my codec

查看:217
本文介绍了QTextCodec子类 - 如何注册我的编解码器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建自己的编解码器,即 QTextCodec 的子类。而且我想使用它通过 QTextCodec :: codecForName(myname);

但是,只是子类是不够的。 QTextCodec :: availableCodecs()不包含我的编解码器名称。

I need to create my own codec, i.e. subclass of QTextCodec. And I'd like to use it via QTextCodec::codecForName("myname");
However, just subclass is not enough. QTextCodec::availableCodecs() does not contain my codec name.

QTextCodec文档不包括正确的注册自定义编解码器:

QTextCodec documentation does not cover the area of proper registration of a custom codec:


创建自己的编解码器类

Creating Your Own Codec Class

支持新的文本编码可以通过创建QTextCodec
子类添加到Qt

Support for new text encodings can be added to Qt by creating QTextCodec subclasses.

纯虚函数描述
系统的编码器并且
编码器根据需要在QBextStream支持的
不同的文本文件格式
中使用,而在X11下,对于
特定于语言环境的字符输入和
输出

The pure virtual functions describe the encoder to the system and the coder is used as required in the different text file formats supported by QTextStream, and under X11, for the locale-specific character input and output.

要将另一个编码的支持添加到
Qt,请创建QTextCodec和
的子类,实现
表中列出的函数以下。

name()

aliases()

mibEnum()

convertToUnicode()

convertFromUnicode ()

To add support for another encoding to Qt, make a subclass of QTextCodec and implement the functions listed in the table below.
name()
aliases()
mibEnum()
convertToUnicode()
convertFromUnicode()

您可能会发现更多c onvenient to
使您的编解码器类可用作
插件;有关详细信息,请参阅如何创建Qt插件

You may find it more convenient to make your codec class available as a plugin; see How to Create Qt Plugins for details.

所以,我试图挖掘一些插件的方向。但是我不想有一个单独的项目与插件。是否可以在同一个项目中声明插件?

So, I've tried to dig a little into plugins' direction. But I don't want to have a separate project with plugin. Is it possible to declare plugin within the same project?

还是直接将编解码器注册到QTextCodec?

Or is there a direct way to register my codec into QTextCodec? This is preferable.

推荐答案

根据qtextcodex.cpp,任何新的编解码器都被添加到已注册编解码器的集合中(* static QList all )由其自己的构造函数。所以创建你的编解码器类的一个实例应该做的诀窍;以下代码对我来说很好:

according to qtextcodex.cpp any new codec is added to the collection of registered codecs (*static QList all) by its own constructor. So creating an instance of your codec class should do the trick; code below worked fine for me:

QMyCodec myCodec;

foreach (QByteArray codecName,  QTextCodec::availableCodecs())
{
    QString codecNameStr(codecName);
    qDebug() << codecNameStr;
}

QTextCodec* codec = QTextCodec::codecForName("MyNewCodec");
if (codec)
{
    qDebug() << "found ";
    qDebug() << codec->name() << '\n';
}

返回的QTextCodec :: availableCodecs:

QTextCodec::availableCodecs returned:


MyNewCodec

系统

roman8
hp-roman8

csHPRoman8...

"MyNewCodec"
"System"
"roman8" "hp-roman8"
"csHPRoman8" ...

QTextCodec :: codecForName返回一个指向我的编解码器类的指针

QTextCodec::codecForName returned a pointer to my codec class

希望这有助于,

这篇关于QTextCodec子类 - 如何注册我的编解码器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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