我有一个QPainterPath被绘制到我的QGraphicsScene,我正在存储路径的点,因为它们被绘制到QList中。
我的问题是我现在如何在绘制这些点时将这些点保存到 xml(我认为这会效果最好(? 我的目标是当应用程序关闭时,我读取了该 xml,然后路径立即重新绘制到场景中。
这是我为写入设置的方法,每次向路径写入新点时,我都会调用该方法。
void writePathToFile(QList pathPoints){
QXmlStreamWriter xml;
QString filename = "../XML/path.xml";
QFile file(filename);
if (!file.open(QFile::WriteOnly | QFile::Text))
qDebug() << "Error saving XML file.";
xml.setDevice(&file);
xml.setAutoFormatting(true);
xml.writeStartDocument();
xml.writeStartElement("path");
// --> no clue what to dump here: xml.writeAttribute("points", ? );
xml.writeEndElement();
xml.writeEndDocument();
}
或者也许这不是最好的方法?
我想我可以处理路径的阅读和重新绘制,但这第一部分欺骗了我。
您可以使用二进制文件:
QPainterPath path;
// do sth
{
QFile file("file.dat");
file.open(QIODevice::WriteOnly);
QDataStream out(&file); // we will serialize the data into the file
out << path; // serialize a path, fortunately there is apriopriate functionality
}
反序列化类似:
QPainterPath path;
{
QFile file("file.dat");
file.open(QIODevice::ReadOnly);
QDataStream in(&file); // we will deserialize the data from the file
in >> path;
}
//do sth