C++ 使用QSettings将自定义类转换为XML
我想使用QSettings将自定义类保存为XML。但是我总是得到没有结构成员的XMLC++ 使用QSettings将自定义类转换为XML,c++,xml,linux,qt4,qsettings,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_
#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
结构接口
{
QString名称;
QString ip;
};
Q_DECLARE_元类型(接口结构)
QDataStream&操作员s.name;
在>>s.ip;
返回;
}
静态bool readXmlFile(QIODevice&device,QSettings::SettingsMap&map)
{
qDebug()据我所知,您将QVariant转换为字符串。将自定义类型转换为字符串没有定义。似乎您想使用QDataStream
,但您不使用它。@谢谢,我会尝试。实际上,现在有一种方法可以定义自定义转换运算符。请参见QMetaType::registerConverter
@peppe说,Qt4中没有这样的函数。我已经更新了这个问题。对,在5.2(?)中。
read
write
read
TEST: "" ""
Press <RETURN> to close this window...
<?xml version="1.0" encoding="UTF-8"?><Settings><network></network></Settings>