2017-04-12 68 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