PDA

View Full Version : XML edit



SombreroMickey
24th February 2015, 11:20
Hi guys,
lḿ writing few rows of code to edit XML like this:


<?xml version="1.0" encoding="UTF-8"?>
<data>
<fam_number>123</fam_number>
</data>

So i google it, then l tought that, this should write xml (loaded by qresource):

QDomDocument doc;
QDomElement xmlroot= doc.createElement ("data");
doc.appendChild (xmlroot);
QStandardItemModel *model = new QStandardItemModel(0,0);
QStandardItem *root = model->item (0,0);
QStandardItem *fam = root->child (0,0); // Here Segmantation FAULT
fam->setText (family_number);

QFile file(":/data.xml");
if(!file.open (QIODevice::WriteOnly | QIODevice::Text)) // Here FAILED to open
qDebug () << "Fail";
QTextStream stream(&file);
stream << doc.toString ();
file.close ();
But l get SEG FAILED and FAILED to open.
Can you please help me ? Thanks

d_stranz
24th February 2015, 15:00
In line 4 of your C++ code, you create a QStandardItemModel with zero rows and zero columns. It has no content. There is no such thing as a "root item" in an empty model, so asking for the item at (0,0) in an empty model will return a NULL pointer (which you don't check for). When you try to use that NULL pointer for something, your code blows up, of course.

If you create an empty model, it is up to you to fill it with QStandardItem instances, which you have to create and append to the model. Th Qt distribution has a lot of examples that use models and views. Study those.

anda_skoa
2nd March 2015, 12:39
And the open for write fails because you are using it on file in a Qt resource, not on a file on disk.

Cheers,
_