使用QSettings自定义类到XML

时间:2015-12-11 12:53:36

标签: c++ xml linux qt4 qsettings

我想使用QSettings将自定义类保存到XML。但我总是得到没有结构成员的XML。

#include <QCoreApplication>
#include <QtCore/qdatastream.h>
#include <qxmlstream.h>
#include <qdebug.h>
#include <QtCore/QSettings>
#include <QMetaType>

struct Interface_struct
{
    QString name;
    QString ip;
};

Q_DECLARE_METATYPE(Interface_struct)

QDataStream& operator <<(QDataStream& out, const Interface_struct& s)
{
    out << s.name << s.ip;
    return out;
}

QDataStream& operator >>(QDataStream& in, Interface_struct& s)
{
    in >> s.name;
    in >> s.ip;
    return in;
}

static bool readXmlFile(QIODevice &device, QSettings::SettingsMap &map)
{
    qDebug()<< "read";
    QXmlStreamReader reader(&device);
    QString key;
    while(!reader.atEnd())
    {
        reader.readNext();
        if( reader.isStartElement() && reader.tokenString() != "Settings")
        {
            if( reader.text().isNull() )
            {
                // key = Settings
                if(key.isEmpty())
                {
                    key = reader.tokenString();
                }
                // key = Settings/Intervall
                else
                {
                    key += "/" + reader.tokenString();
                }
            }
            else
            {
                map.insert(key, reader.text().data());
            }
        }
    }

    return true;
}

static bool writeXmlFile(QIODevice &device, const QSettings::SettingsMap &map)
{
    qDebug()<< "write";
    QXmlStreamWriter writer(&device);
    writer.writeStartDocument("1.0");
    writer.writeStartElement("Settings");
    foreach(QString key, map.keys())
    {
        foreach(QString elementKey, key.split("/"))
        {
            writer.writeStartElement(elementKey);
        }
        writer.writeCharacters(map.value(key).toString());
        writer.writeEndElement();
    }
    writer.writeEndElement();
    writer.writeEndDocument();

    return true;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    qRegisterMetaType<Interface_struct>("Interface_struct");
    qRegisterMetaTypeStreamOperators<Interface_struct>("Interface_struct");

    {
        Interface_struct s;
        s.name = QString("br03000");
        s.ip = QString("172.16.222.5");
        const QSettings::Format xml_format =
                QSettings::registerFormat("xml", readXmlFile, writeXmlFile);
        if(xml_format == QSettings::InvalidFormat)
        {
            qDebug() << "InvalidFormat!";
            return 0;
        }

        QSettings::setPath(xml_format, QSettings::UserScope, "/home/farit/test/");
        QSettings settings(xml_format, QSettings::UserScope, "xml_cfg");
        settings.setValue("network", QVariant::fromValue(s));
    }

    {
        QSettings::Format xml_format =
                QSettings::registerFormat("xml", readXmlFile, writeXmlFile);
        QSettings::setPath(xml_format, QSettings::UserScope, "/home/farit/test/");
        QSettings settings(xml_format, QSettings::UserScope, "xml_cfg");
        QVariant value = settings.value("network");
        Interface_struct interface = value.value<Interface_struct>();
        qDebug() << "TEST: " << interface.name << interface.ip;
    }
return 0;
}

我得到了这个输出:

read 
write 
read 
TEST:  "" "" 
Press <RETURN> to close this window...

XML看起来像这样:

<?xml version="1.0" encoding="UTF-8"?><Settings><network></network></Settings>

如何使用QSettings将自定义类的结构成员保存到XML?

更新:对不起,我忘了提及,这应该是在Qt4中完成的。

0 个答案:

没有答案