English 中文(简体)
QTextCodec subclass - how to register my codec
原标题:

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 documentation does not cover the area of proper registration of a custom codec:

Creating Your Own Codec Class

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

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.

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()

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?

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

最佳回答

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() <<  
 ;
}

QTextCodec::availableCodecs returned:

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

QTextCodec::codecForName returned a pointer to my codec class

hope this helps, regards

问题回答

暂无回答




相关问题
Mojarra for JSF Encoding

Can anyone teach me how to use mojarra to encode my JSF files. I downloaded mojarra and expected some kind of jar but what i had downloaded was a folder of files i don t know what to do with

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Using Java PDFBox library to write Russian PDF

I am using a Java library called PDFBox trying to write text to a PDF. It works perfect for English text, but when i tried to write Russian text inside the PDF the letters appeared so strange. It ...

what is encoding in Ajax?

Generally we are using UTF-8 encoding standard for sending the request for every language. But in some language this encoding standard is not working properly,then in that case we are using ISO-8859-1....

Encoding of window.location.hash

Does window.location.hash contain the encoded or decoded representation of the url part? When I open the same url (http://localhost/something/#%C3%BC where %C3%BCtranslates to ü) in Firefox 3.5 and ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

热门标签