在QT中将配置设置保存到XML文件?

时间:2011-07-06 08:46:50

标签: qt configuration settings configuration-files

我想在QT中将配置设置保存到XML文件并从那里读取它?

在QT中执行此操作的最佳方法是什么?

非常感谢任何样本/想法。

感谢。

4 个答案:

答案 0 :(得分:6)

您可以使用QSettings::registerFormat

注册XML文件格式

以下是readXmlFile and writeXmlFile

的示例实现

答案 1 :(得分:2)

There是XML的Qt示例。

您也可以使用QSettings存储设置。

答案 2 :(得分:1)

这是我将设置导出到XML的代码。你需要的是打开ExportSettingsToXml()打开来写QFile。可以使用ImportSettingsFromXml()再次导入导出的文件。

QString Variant2Text( const QVariant &value )
{
    switch (value.type())
    {
    case QVariant::String:
    case QVariant::Int:
    case QVariant::UInt:
    case QVariant::LongLong:
    case QVariant::ULongLong:
    case QVariant::Double:
    case QVariant::Bool:
        return value.toString();
    }

    QByteArray ba;
    value.save(QDataStream(&ba, QIODevice::WriteOnly));

    return QString(ba.toHex());
}

QVariant Text2Variant( const QString &sText, QVariant::Type nType )
{
    switch (nType)
    {
    case QVariant::String:
    case QVariant::Int:
    case QVariant::UInt:
    case QVariant::LongLong:
    case QVariant::ULongLong:
    case QVariant::Double:
    case QVariant::Bool:
        {
            QVariant var(sText);
            if (var.convert(nType))
                return var;

            return QVariant(sText);
        }
        return QVariant::fromValue(sText);
    }

    QVariant value;
    value.load(QDataStream(&QByteArray::fromHex(sText.toLatin1()), QIODevice::ReadOnly));

    return value;
}

void XmlExportSettingsRecursive(QXmlStreamWriter &writer, QSettings &settings, const QStringList &blackList)
{
    QString sGroup = settings.group();

    if (!sGroup.isEmpty())
    {
        writer.writeStartElement("group");  
        writer.writeAttribute(QStringLiteral("name"), sGroup.mid(sGroup.indexOf(QChar('/')) + 1));
    }
    // Write child groups first
    {
        const QStringList &listGroups = settings.childGroups();
        foreach (const QString &group, listGroups)
        {
            settings.beginGroup(group);         
            XmlExportSettingsRecursive(writer, settings, blackList);
            settings.endGroup();
        }
    }

    const QStringList &listKeys = settings.childKeys();
    foreach (const QString &key, listKeys)
    {
        if (blackList.contains(key))
            continue;

        writer.writeStartElement("key");
        writer.writeAttribute(QStringLiteral("name"), key);
        const QVariant &value = settings.value(key);
        writer.writeAttribute(QStringLiteral("type"), value.typeName());
        writer.writeCharacters(Variant2Text(value));
        writer.writeEndElement();
    }

    if (!sGroup.isEmpty())
        writer.writeEndElement();
}

bool XmlImportSettingsRecursive(QXmlStreamReader &reader, QSettings &settings)
{
    while (!reader.atEnd()) {
        reader.readNext();
        if (reader.isStartElement())
        {           
            if (reader.name() == QStringLiteral("group"))
            {
                const QXmlStreamAttributes &attr = reader.attributes();
                QString sGroup = attr.value(QStringLiteral("name")).toString();

                settings.beginGroup(sGroup);
                if (!XmlImportSettingsRecursive(reader, settings))
                    return false;
                settings.endGroup();
            }
            else if (reader.name() == QStringLiteral("key"))
            {
                const QXmlStreamAttributes &attr = reader.attributes();
                const QString &sKey = attr.value(QStringLiteral("name")).toString();
                const QString &sType = attr.value(QStringLiteral("type")).toString();
                if (reader.readNext())
                {
                    if (reader.isCharacters() || reader.isEndElement())
                    {
                        const QString &sValue = reader.text().toString();
                        settings.setValue(sKey, Text2Variant(sValue, QVariant::nameToType(sType.toLatin1())));
                    }
                    else
                        continue;
                }
            }
        }
        else if (reader.isEndElement() && reader.name() == QStringLiteral("group"))
            return true;
    }
    if (reader.hasError()) {
        qDebug()<<reader.errorString();
        return false;
    }

    return true;
}

bool ExportSettingsToXml( QIODevice *device, const QString &settingsGroup = QString(), const QStringList &blackList = QStringList())
{   
    if (!device || !device->isWritable())
        return false;

    QSettings settings;
    settings.beginGroup(settingsGroup);

    QXmlStreamWriter xmlWriter( device );
    xmlWriter.setAutoFormatting( true );
    xmlWriter.writeStartDocument();

    xmlWriter.writeStartElement( QStringLiteral("settings") );

    if (!settingsGroup.isEmpty())
    {   
        xmlWriter.writeStartElement(QStringLiteral("group"));
        xmlWriter.writeAttribute(QStringLiteral("name"), settingsGroup);
    }

    XmlExportSettingsRecursive(xmlWriter, settings, blackList);

    if (!settingsGroup.isEmpty())
        xmlWriter.writeEndElement();

    xmlWriter.writeEndDocument();

    return true;
}

bool ImportSettingsFromXml( QIODevice *device, const QString &settingsGroup = QString())
{
    if (!device || !device->isReadable())
    {
        qDebug()<<"Trying to import XML settings from non-readable device";
        return false;
    }

    QSettings settings;
    settings.beginGroup(settingsGroup);
    QXmlStreamReader xmlReader( device );
    xmlReader.readNext();
    if (!xmlReader.isStartDocument())
    {
        qDebug()<<"Invalid xml document format";
        return false;
    }

    if (!xmlReader.readNextStartElement())
    {
        qDebug()<<"Empty settings xml document";
        return true;
    }

    if (xmlReader.name() == QStringLiteral("settings") )
    {
        if (!XmlImportSettingsRecursive(xmlReader, settings))
            return false;
    }
    else
    {
        qDebug()<<"Unknown settings format.";
        return false;
    }

    return true;
}

答案 3 :(得分:0)

对于这些事情,我经常使用boost library来表示C +,并且可以强烈推荐它。它提供了各种格式的序列化,如XML,文本,二进制等。

Serialization Tutorial(实际上看起来更复杂)