0
votes

I have a QPainterPath that is being drawn to my QGraphicsScene and I am storing the points of the path as they are drawn into a QList.

My question is how do I now save those points out to an xml ( I assume this would work best ) as they are drawn? My goal is when the app closes, I read that xml, and the path is immediately re-drawn into the scene.

Here is the method I have setup for the writing, which I would call everytime I write a new point to the path.

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();
}

Or maybe this isn't the best way to go about this?

I think I can handle the reading and re-drawing of the path, but this first part is tricking me up.

1
XML is ment to be human-readable data. Why won't you consider binary data file?jaskmar
Hmm, I agree, I guess xml is pointless in this context. The question still remains though, not sure how I output the points.bauervision

1 Answers

3
votes

You may use binary file:

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
}

Deserialization is similar:

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