我有一个QPainterPath,它正在被绘制到我的QGraphicsScene上,当它们被绘制到一个QList中时,我会存储路径的点。
我的问题是,在绘制xml时,如何将这些点保存到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();
}或者这不是最好的方法?
我想我可以处理这条路的阅读和重新绘制,但第一部分是欺骗我。
发布于 2017-04-12 13:31:37
您可以使用二进制文件:
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 sthhttps://stackoverflow.com/questions/43370731
复制相似问题