Results 1 to 12 of 12

Thread: make trewidget's xml file

  1. #1
    Join Date
    Dec 2010
    Location
    Lithuania
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: make trewidget's xml file

    I have tree widget. After clicking create button I want that there would be created xml file, which has tree widget's data. Is it possible to do that? If so maybe somebody could tell me how to do that?


    Added after 1 8 minutes:


    I found some examples in few books. but when I trie to use them few errors

    there goes examples:
    Qt Code:
    1. bool writeXml(const QString &fileName, QTreeWidget *treeWidget)
    2. {
    3. QFile file(fileName);
    4. if (!file.open(QFile::WriteOnly | QFile::Text)) {
    5. std::cerr << "Error: Cannot write file "
    6. << qPrintable(fileName) << ": "
    7. << qPrintable(file.errorString()) << std::endl;
    8. return false;
    9. }
    10. QXmlStreamWriter xmlWriter(&file);
    11. xmlWriter.setAutoFormatting(true);
    12. xmlWriter.writeStartDocument();
    13. xmlWriter.writeStartElement("bookindex");
    14. for (int i = 0; i < treeWidget->topLevelItemCount(); ++i)
    15. writeIndexEntry(&xmlWriter, treeWidget->topLevelItem(i));
    16. xmlWriter.writeEndDocument();
    17. file.close();
    18. if (file.error()) {
    19. std::cerr << "Error: Cannot write file "
    20. << qPrintable(fileName) << ": "
    21. << qPrintable(file.errorString()) << std::endl;
    22. return false;
    23. }
    24. return true;
    25. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void writeIndexEntry(QXmlStreamWriter *xmlWriter, QTreeWidgetItem *item)
    2. {
    3. xmlWriter->writeStartElement("entry");
    4. xmlWriter->writeAttribute("term", item->text(0));
    5. QString pageString = item->text(1);
    6. if (!pageString.isEmpty()) {
    7. QStringList pages = pageString.split(", ");
    8. foreach (QString page, pages)
    9. xmlWriter->writeTextElement("page", page);
    10. }
    11. for (int i = 0; i < item->childCount(); ++i)
    12. writeIndexEntry(xmlWriter, item->child(i));
    13. xmlWriter->writeEndElement();
    14. }
    To copy to clipboard, switch view to plain text mode 

    So errors that ocurred is with std::cerr and another error that QFile file(fileName); file name wasn't described.

    By the way in this example if I do understand it correctly there should be created file before tree is placed there?
    Last edited by Shien; 1st January 2011 at 15:43.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: make trewidget's xml file

    To use std::cerr, include the iostream header. Or, alternatively, use qDebug.

    As for the file being created:
    To quote the documentation:
    Note: In WriteOnly or ReadWrite mode, if the relevant file does not already exist, this function will try to create a new file before opening it.
    Can you post the exact errors? This way it is easier to give specific help.

    Note too that the saving of the tree in the example above is very specific to a certain structure.
    You should use some sort of general recursive function.

  3. #3
    Join Date
    Dec 2010
    Location
    Lithuania
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: make trewidget's xml file

    I found one more example
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "xbelwriter.h"
    4.  
    5. XbelWriter::XbelWriter(QTreeWidget *treeWidget)
    6. : treeWidget(treeWidget)
    7. {
    8. xml.setAutoFormatting(true);
    9. }
    10.  
    11. bool XbelWriter::writeFile(QIODevice *device)
    12. {
    13. xml.setDevice(device);
    14.  
    15. xml.writeStartDocument();
    16. xml.writeDTD("<!DOCTYPE xbel>");
    17. xml.writeStartElement("xbel");
    18. xml.writeAttribute("version", "1.0");
    19. for (int i = 0; i < treeWidget->topLevelItemCount(); ++i)
    20. writeItem(treeWidget->topLevelItem(i));
    21.  
    22. xml.writeEndDocument();
    23. return true;
    24. }
    25.  
    26. void XbelWriter::writeItem(QTreeWidgetItem *item)
    27. {
    28. QString tagName = item->data(0, Qt::UserRole).toString();
    29. if (tagName == "folder") {
    30. bool folded = !treeWidget->isItemExpanded(item);
    31. xml.writeStartElement(tagName);
    32. xml.writeAttribute("folded", folded ? "yes" : "no");
    33. xml.writeTextElement("title", item->text(0));
    34. for (int i = 0; i < item->childCount(); ++i)
    35. writeItem(item->child(i));
    36. xml.writeEndElement();
    37. } else if (tagName == "bookmark") {
    38. xml.writeStartElement(tagName);
    39. if (!item->text(1).isEmpty())
    40. xml.writeAttribute("href", item->text(1));
    41. xml.writeTextElement("title", item->text(0));
    42. xml.writeEndElement();
    43. } else if (tagName == "separator") {
    44. xml.writeEmptyElement(tagName);
    45. }
    46. }
    To copy to clipboard, switch view to plain text mode 

    but I don't understand it.
    Qt Code:
    1. bool XbelWriter::writeFile(QIODevice *device)
    2. {
    3. xml.setDevice(device);
    To copy to clipboard, switch view to plain text mode 
    does that means file name?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: make trewidget's xml file

    A QIODevice can mean any device that is able to read, write or both.
    A serial port for example, a printer, a usb memory stick, a tcp socket. But in most general cases it is just a file.
    A QFile is a QIODevice. So you can just pass your QFile object to the writeFile function.

    I guess the creators of this function had other devices in mind besides a QFile, like a network connection.

  5. #5
    Join Date
    Dec 2010
    Location
    Lithuania
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: make trewidget's xml file

    But this example is only for one column tree widget? I have 2 column tree widget.

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: make trewidget's xml file

    The last example uses 2 columns.
    See the text(0) and text(1) function calls in the example.

  7. #7
    Join Date
    Dec 2010
    Location
    Lithuania
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: make trewidget's xml file

    I found something that i need but the problem is that I can't align as I want.
    Qt Code:
    1. for (int i = 0; i < treeW->topLevelItemCount(); ++i)
    2. {
    3. xmlWriter.writeEmptyElement(treeW->topLevelItem(i)->text(0));
    4. xmlWriter.writeTextElement("Type",treeW->topLevelItem(i)->text(1));
    5. xmlWriter.writeTextElement("Resource",treeW->topLevelItem(i)->text(2));
    6. }
    To copy to clipboard, switch view to plain text mode 

    this code would make :
    Qt Code:
    1. <Dwelling: 1/>
    2. <Type>Liquid</Type>
    3. <Resource>item1</Resource>
    4. <Dwelling: 2/>
    5. <Type>Etherial</Type>
    6. <Resource>item1</Resource>
    To copy to clipboard, switch view to plain text mode 

    But I need something like this:
    Qt Code:
    1. <Dwelling: 1>
    2. <Type>Liquid</Type>
    3. <Resource>item1</Resource>
    4. </Dwelling: 1>
    5. <Dwelling: 2/>
    6. <Type>Etherial</Type>
    7. <Resource>item1</Resource>
    8. </Dwelling: 2>
    To copy to clipboard, switch view to plain text mode 

    how to make this?

  8. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: make trewidget's xml file

    I guess you need a start and end element.
    See the documentation

  9. #9
    Join Date
    Dec 2010
    Location
    Lithuania
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: make trewidget's xml file

    but I already have one
    those elements are inside of it.

  10. #10
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: make trewidget's xml file

    Then you don't use them correctly I guess.

  11. #11
    Join Date
    Dec 2010
    Location
    Lithuania
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: make trewidget's xml file

    no I needed writeStartElement. Thanks

  12. #12
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: make trewidget's xml file

    I do not think that <Dwelling: 1> and so forth is correct XML syntax. If you are trying to enumerate things, then this syntax would be correct:

    <Dwelling unit="1">
    </Dwelling>

    In other words, you can't put arbitrary text into elements. "Dwelling:" is not an acceptable element name, and the numeral "1" all by itself has no meaning in XML.

    Your elements must have the form: <ElementName attributeName="attributeValue> </ElementName>
    or <ElementName attributeName="attrbuteValue" />

    In the first case, you can have child elements between the <ElementName> and </ElementName> start and end tags. In the second case, the entire element is contained within the tag - you can't have child elements inside it.

    A proper XML writer will enforce correct syntax. If you write out the XML by hand, then what you have written will not pass a syntax check by a real XML parser.

Similar Threads

  1. Replies: 5
    Last Post: 30th August 2011, 23:21
  2. How to make a file browser like explorer?
    By alexivanov91 in forum Qt Programming
    Replies: 5
    Last Post: 18th August 2010, 10:52
  3. qmake doesn't make a 'make' file
    By elyness in forum Installation and Deployment
    Replies: 1
    Last Post: 4th August 2010, 15:21
  4. QFtp->get() traversing trewidget
    By LordQt in forum Qt Programming
    Replies: 8
    Last Post: 20th March 2008, 14:23
  5. How to make qpf file
    By noufalk in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 10th March 2008, 21:16

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.