我有一个正在绘制到我的 QGraphicsScene 的 QPainterPath,并且我正在存储绘制到 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
关于c++ - 将 QPainterPath 写入 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43370731/