Results 1 to 4 of 4

Thread: Save TreeWidget into XML-FIle

  1. #1
    Join Date
    Jul 2015
    Location
    Austria
    Posts
    20
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Question Save TreeWidget into XML-FIle

    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?
    Qt Code:
    1. void ImageModul::writeIndexEntry(QXmlStreamWriter *xmlWriter, QTreeWidgetItem *item)
    2. {
    3. xmlWriter->writeStartElement("attribute");
    4. xmlWriter->writeAttribute("", item->text(0));
    5. QString valueString = item->text(1);
    6. if (!valueString.isEmpty()) {
    7. QStringList values = valueString.split(", ");
    8. foreach (QString value, values)
    9. xmlWriter->writeTextElement("value", value);
    10. }
    11. for (int i = 0; i < item->childCount(); ++i)
    12. writeIndexEntry(xmlWriter, item->child(i));
    13. xmlWriter->writeEndElement();
    14. }
    15. bool ImageModul::writeXml(const QString &fileName, QTreeWidget *treeWidget)
    16. {
    17. QFile file(fileName);
    18. if (!file.open(QFile::WriteOnly | QFile::Text)) {
    19. qDebug() << "Error: Cannot write file ";
    20. return false;
    21. }
    22.  
    23. QXmlStreamWriter xmlWriter(&file);
    24. xmlWriter.setAutoFormatting(true);
    25. xmlWriter.writeStartDocument();
    26. xmlWriter.writeStartElement("cell");
    27. for (int i = 0; i < treeWidget->topLevelItemCount(); ++i)
    28. writeIndexEntry(&xmlWriter, treeWidget->topLevelItem(i));
    29. xmlWriter.writeEndDocument();
    30. file.close();
    31. if (file.error()) {
    32. qDebug() << "Error: Cannot write file ";
    33. return false;
    34. }
    35. return true;
    36. }
    To copy to clipboard, switch view to plain text mode 

    Unbenannt.PNG
    (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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Save TreeWidget into XML-FIle

    You already seem to handle the value in the second column?

    Cheers,
    _

  3. #3
    Join Date
    Jul 2015
    Location
    Austria
    Posts
    20
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Save TreeWidget into XML-FIle

    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

    Qt Code:
    1. writeXml("D:/file.txt",parentTreeWidget);
    To copy to clipboard, switch view to plain text mode 

    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:
    Unbenannt.PNG

    I create the treewidget-entries with this custom method:

    Qt Code:
    1. void ImageModul::addTreeEntry(QString headerItemString,QStringList itemNameString,QWidget *widgets[],int arrayLenght)
    2. {
    3. QFont font("Helvetica ", 10, QFont::Bold);
    4. QLineEdit *nameLineEdit = new QLineEdit();
    5. nameLineEdit->setStyleSheet("QLineEdit { qproperty-frame: false }");
    6. nameLineEdit->setText(headerItemString);
    7. nameLineEdit->setFont(font);
    8. QTreeWidgetItem *HeaderItem = new QTreeWidgetItem(parentTreeWidget);
    9. parentTreeWidget->setItemWidget(HeaderItem,0,nameLineEdit);
    10. HeaderItem->setFirstColumnSpanned(true);
    11.  
    12. for(int i=0;i<arrayLenght;i++)
    13. {
    14. QTreeWidgetItem *treeItem = new QTreeWidgetItem(HeaderItem);
    15. treeItem->setText(0,itemNameString.at(i));
    16. treeItem->setText(1," ");
    17. parentTreeWidget->setItemWidget(treeItem,1,widgets[i]);
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 


    And in the contructor
    Qt Code:
    1. ImageModul::ImageModul(BaseWidget *parent,QTreeWidget *p) :
    2. BaseWidget(parent)
    3. {
    4. parentTreeWidget=p;
    5. imageName=new QLineEdit();
    6. imagePath=new QLineEdit();
    7. imageTime=new QSpinBox();
    8. imageVisible=new QCheckBox();
    9. imageStrech=new QCheckBox();
    10. imageDirectory=new QPushButton("search...");
    11.  
    12.  
    13. rows<<"Path"<<"Find Images"<<"Duration"<<"Visibility"
    14. <<"strech Images";
    15. QWidget *w[5]= {imagePath,imageDirectory,imageTime,imageVisible,imageStrech};
    16. addTreeEntry("Images",rows,w,5);
    To copy to clipboard, switch view to plain text mode 
    Last edited by mayrhofer; 27th December 2015 at 13:30.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Save TreeWidget into XML-FIle

    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,
    _

Similar Threads

  1. How to save a file in UTF with BOM?
    By Alan_K in forum Newbie
    Replies: 6
    Last Post: 19th March 2011, 22:32
  2. Replies: 2
    Last Post: 18th May 2010, 23:44
  3. Save File in another Folder
    By GonzoFist in forum General Programming
    Replies: 14
    Last Post: 20th April 2010, 13:59
  4. Save file dialog
    By yxtx1984 in forum Newbie
    Replies: 1
    Last Post: 22nd January 2010, 12:00
  5. Replies: 1
    Last Post: 28th July 2009, 07:58

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.