2

所以,我需要使这个函数的结果是唯一的。

我的任务很简单:我只想显示文本处理应用程序的编码选择对话框。我的第一个天真的解决方案如下所示:

QList<QByteArray> encodings = QTextCodec::availableCodecs ();
QSet<QTextCodec*> unique_codecs;
unique_codecs.insert (QTextCodec::codecForName ("autodetect"));
unique_codecs.insert (QTextCodec::codecForName ("system"));
foreach (QByteArray e, encodings)
{
    if (QTextCodec* c = QTextCodec::codecForName (e))
    {
        if (!unique_codecs.contains (c))
        {
            m_encodings.append (e);    // QStringList to store encodings
            unique_codecs.insert (c);
        }
    }
}

你还有其他建议吗?

4

2 回答 2

3

availableMibs()一些方法可以返回 MIB 列表——它们是编解码器的唯一 ID,不会有重复项。QTextCodec::codecForMib(int)然后将让您检索有问题的编解码器。

foreach (int mib, QTextCodec::availableMibs()) {
    m_encodings << QTextCodec::codecForMib(mib)->name();
}

在我的机器上,我得到 111 个可用的 mib 和 804 个可用的编解码器名称

于 2013-10-26T17:16:38.170 回答
1

The answer to the question (how to make the list with unique items with the help of availableMibs()) was given, so here is the addition to that - why there were duplicates.

QTextCodec::availableCodecs() returns a list of codecs with all popular aliases, so, for example, the codec cp-1252 will be present as two aliases (or more, if any): cp-1252 and windows-1252. It can be useful if the user needs to choose the encoding via combo-like list with automatic item filtration. He can type cp-1252 or windows-1252 and have the same result even if he doesn't remember the alternative alias.

于 2013-10-27T08:53:13.417 回答