1、保存到xml文件:
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Qt Drawing"),
tr("Cannot write file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return false;
}
QXmlStreamWriter xml(&file);
xml.setAutoFormatting(true);
xml.writeStartDocument();
xml.writeDTD("<!DOCTYPE qdraw>");
xml.writeStartElement("canvas");
xml.writeAttribute("width",QString("%1").arg(scene()->width()));
xml.writeAttribute("height",QString("%1").arg(scene()->height()));
foreach (QGraphicsItem *item , scene()->items()) {
AbstractShape * ab = qgraphicsitem_cast<AbstractShape*>(item);
QGraphicsItemGroup *g = dynamic_cast<QGraphicsItemGroup*>(item->parentItem());
if ( ab &&!qgraphicsitem_cast<SizeHandleRect*>(ab) && !g ){
ab->saveToXml(&xml);
}
}
xml.writeEndElement();
xml.writeEndDocument();
2从文件中读取
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Qt Drawing"),
tr("Cannot read file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return false;
}
QXmlStreamReader xml(&file);
if (xml.readNextStartElement()) {
if ( xml.name() == tr("canvas"))
{
int width = xml.attributes().value(tr("width")).toInt();
int height = xml.attributes().value(tr("height")).toInt();
scene()->setSceneRect(0,0,width,height);
loadCanvas(&xml);
}
}
setCurrentFile(fileName);
qDebug()<<xml.errorString();
return !xml.error();
3、item对象存为xml对象
bool GraphicsItemGroup::saveToXml(QXmlStreamWriter *xml)
{
xml->writeStartElement("group");
xml->writeAttribute(tr("x"),QString("%1").arg(pos().x()));
xml->writeAttribute(tr("y"),QString("%1").arg(pos().y()));
xml->writeAttribute(tr("rotate"),QString("%1").arg(rotation()));
foreach (QGraphicsItem * item , childItems()) {
removeFromGroup(item);
AbstractShape * ab = qgraphicsitem_cast<AbstractShape*>(item);
if ( ab &&!qgraphicsitem_cast<SizeHandleRect*>(ab)){
ab->updateCoordinate();
ab->saveToXml(xml);
}
addToGroup(item);
}
xml->writeEndElement();
return true;
}
4、item对象从xml中读取属性
bool GraphicsRectItem::loadFromXml(QXmlStreamReader * xml )
{
m_isRound = (xml->name() == tr("roundrect"));
if ( m_isRound ){
m_fRatioX = xml->attributes().value(tr("rx")).toDouble();
m_fRatioY = xml->attributes().value(tr("ry")).toDouble();
}
readBaseAttributes(xml);
updateCoordinate();
xml->skipCurrentElement();
return true;
}
5、从xml对象中读取item对象并加入scene中。
void DrawView::loadCanvas( QXmlStreamReader *xml)
{
Q_ASSERT(xml->isStartElement() && xml->name() == "canvas");
while (xml->readNextStartElement()) {
AbstractShape * item = NULL;
if (xml->name() == tr("rect")){
item = new GraphicsRectItem(QRect(0,0,1,1));
}else if (xml->name() == tr("roundrect")){
item = new GraphicsRectItem(QRect(0,0,1,1),true);
}else if (xml->name() == tr("ellipse"))
item = new GraphicsEllipseItem(QRect(0,0,1,1));
else if (xml->name()==tr("polygon"))
item = new GraphicsPolygonItem();
else if ( xml->name()==tr("bezier"))
item = new GraphicsBezier();
else if ( xml->name() == tr("polyline"))
item = new GraphicsBezier(false);
else if ( xml->name() == tr("line"))
item = new GraphicsLineItem();
else if ( xml->name() == tr("group"))
item =qgraphicsitem_cast<AbstractShape*>(loadGroupFromXML(xml));
else
xml->skipCurrentElement();
if (item && item->loadFromXml(xml))
scene()->addItem(item);
else if ( item )
delete item;
}
}
大概就这么个过程吧。