I have a QGraphicsScene where I am drawing a QPainterPath, and I need to be able to save the shape, and redraw it when the app runs again. Here is how I'm drawing the shape, simplified version, and my write method.
void drawPath(){
QPoint p1 = QPoint(10, 20);
writePointsToFile(p1);
QPoint p2 = QPoint(25, 30);
writePointsToFile(p2);
QPoint p3 = QPoint(40, 60);
writePointsToFile(p3);
QPainterPath path;
path.moveTo(p1.x(), p1.y());
path.lineTo(p2.x(), p2.y());
path.lineTo(p3.x(), p3.y());
}
void writePointsToFile(QPoint point){
QFile file("../path.dat");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out << point;
file.close();
}
Currently, my file is never written to when it runs.
But beyond that, is the correct way to serialize this data so that I can rebuild the shape?
I thought I would be able to handle re-drawing, but I'm not understanding the serialization well enough.
Do I serialize the points? The List containing the points?
My thought was if I serialize the points, when I deserialize, I then add them to a list, and I should be able to recreate the shape based on the position of each point in the list; ie point at position 0 would be p1, point at 1 would be p2, etc. But I can't get this far because nothing is being written to the file anyway. Plus I'm not entirely sure what to expect from the serialization of the data in the first place.
Any help on this would be wonderful.
EDIT: based on feedback, I am now trying to this in my write method
QFile file("../path.dat");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
QDataStream & operator << (QDataStream& file, const QPainterPath& path);
out << path;
file.close();
This compiles fine, even though I'm not entirely sure I'm doing that right, nothing is being written to the file so I assume I'm still off somewhere.