2017-04-12 72 views
0

我有一个正在绘制到我的QGraphicsScene的QPainterPath,并且我正在将它们绘制到QList中时存储路径的点。将QPainterPath写入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(); 
} 

或者,也许这不是最好的方式去做这件事?

我想我可以处理阅读和重新绘制的路径,但这第一部分欺骗了我。

+0

XML是换货是人类可读的数据。你为什么不考虑二进制数据文件? – jaskmar

+0

嗯,我同意,我猜在这种情况下xml是毫无意义的。问题仍然存在,但不知道如何输出点。 – bauervision

回答

3

您可以使用二进制文件:

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