PDA

View Full Version : Save TreeWidget into XML-FIle



mayrhofer
27th December 2015, 10:14
I want so save a QTreeWidget into a XML-File.(And open it later)I found 2 functions where i can save strings, but i can´t save values from widgets. So how i can save the values for exammple of a QTextEdit?

void ImageModul::writeIndexEntry(QXmlStreamWriter *xmlWriter, QTreeWidgetItem *item)
{
xmlWriter->writeStartElement("attribute");
xmlWriter->writeAttribute("", item->text(0));
QString valueString = item->text(1);
if (!valueString.isEmpty()) {
QStringList values = valueString.split(", ");
foreach (QString value, values)
xmlWriter->writeTextElement("value", value);
}
for (int i = 0; i < item->childCount(); ++i)
writeIndexEntry(xmlWriter, item->child(i));
xmlWriter->writeEndElement();
}
bool ImageModul::writeXml(const QString &fileName, QTreeWidget *treeWidget)
{
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
qDebug() << "Error: Cannot write file ";
return false;
}

QXmlStreamWriter xmlWriter(&file);
xmlWriter.setAutoFormatting(true);
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement("cell");
for (int i = 0; i < treeWidget->topLevelItemCount(); ++i)
writeIndexEntry(&xmlWriter, treeWidget->topLevelItem(i));
xmlWriter.writeEndDocument();
file.close();
if (file.error()) {
qDebug() << "Error: Cannot write file ";
return false;
}
return true;
}

11592
(saving path,duration,visibily,strech Images - Values into a XML *.txt file)

I need it to save several TreeWidgets(which are in a 2d vector) into a file.

anda_skoa
27th December 2015, 11:59
You already seem to handle the value in the second column?

Cheers,
_

mayrhofer
27th December 2015, 12:13
Yes the value is in the 2nd column. The description is in the 1st column. And the Problem is that i only get the desc in the XML-File because it´s a QString .

I call the Function with this code


writeXml("D:/file.txt",parentTreeWidget);

I get this Output: (attribute=description and the cell will be a id of a TableWidget-Cell).


<?xml version="1.0" encoding="UTF-8"?>
<cell>
<attribute ="">
<attribute ="Path">
<value> </value>
</attribute>
<attribute ="Find Images">
<value> </value>
</attribute>
<attribute ="Duration">
<value> </value>
</attribute>
<attribute ="Visibility">
<value> </value>
</attribute>
<attribute ="strech Images">
<value> </value>
</attribute>
</attribute>
</cell>
It ignores the Top-Level <attribute=""> also. (because it´s a QLineEdit)
when the user input is this:
11593

I create the treewidget-entries with this custom method:



void ImageModul::addTreeEntry(QString headerItemString,QStringList itemNameString,QWidget *widgets[],int arrayLenght)
{
QFont font("Helvetica ", 10, QFont::Bold);
QLineEdit *nameLineEdit = new QLineEdit();
nameLineEdit->setStyleSheet("QLineEdit { qproperty-frame: false }");
nameLineEdit->setText(headerItemString);
nameLineEdit->setFont(font);
QTreeWidgetItem *HeaderItem = new QTreeWidgetItem(parentTreeWidget);
parentTreeWidget->setItemWidget(HeaderItem,0,nameLineEdit);
HeaderItem->setFirstColumnSpanned(true);

for(int i=0;i<arrayLenght;i++)
{
QTreeWidgetItem *treeItem = new QTreeWidgetItem(HeaderItem);
treeItem->setText(0,itemNameString.at(i));
treeItem->setText(1," ");
parentTreeWidget->setItemWidget(treeItem,1,widgets[i]);
}
}



And in the contructor


ImageModul::ImageModul(BaseWidget *parent,QTreeWidget *p) :
BaseWidget(parent)
{
parentTreeWidget=p;
QStringList rows;
imageName=new QLineEdit();
imagePath=new QLineEdit();
imageTime=new QSpinBox();
imageVisible=new QCheckBox();
imageStrech=new QCheckBox();
imageDirectory=new QPushButton("search...");


rows<<"Path"<<"Find Images"<<"Duration"<<"Visibility"
<<"strech Images";
QWidget *w[5]= {imagePath,imageDirectory,imageTime,imageVisible,i mageStrech};
addTreeEntry("Images",rows,w,5);

anda_skoa
27th December 2015, 13:28
Ok, I see.

What you need to do is getting the item widget for the item and then accessing its "value" property.
You can either store the name of that property in the item, e.g. "text" for the line edit, or use the widget's user property QMetaObject::userProperty() which should be the "value" property for all these editor widgets.

Btw, writing an empty attribute name is not likely to be valid XML.

Cheers,
_