Sirs
I am having a problem displaying a Qlist of QTreeWidgetItems in a QTreeWidget.
Code follows:-

domparser.h
Qt Code:
  1. #ifndef DOMPARSER_H
  2. #define DOMPARSER_H
  3.  
  4. #include <QTreeWidget>
  5. #include <QtXml/QDomElement>
  6. #include <QTreeWidgetItem>
  7. #include <QMessageBox>
  8. #include <utilities.h>
  9. #include <utils.h>
  10.  
  11. class domParser
  12. {
  13. public:
  14. domParser(QIODevice *device, QStringList nodeList, QStringList attrbList);
  15. QList<QTreeWidgetItem *> items;
  16.  
  17. private:
  18. QStringList listOfNodes, listOfAttrbs;
  19. };
  20.  
  21. #endif // DOMPARSER_H
To copy to clipboard, switch view to plain text mode 

domparser.cpp
Qt Code:
  1. #include "domparser.h"
  2.  
  3. domParser::domParser(QIODevice *device, QStringList nodeList, QStringList attrbList)
  4. {
  5. listOfNodes = nodeList;
  6. listOfAttrbs = attrbList;
  7. int column = 0;
  8.  
  9. QString errorStr, nodeName;
  10. int errorLine;
  11. int errorColumn;
  12.  
  13. if (!doc.setContent(device, true, &errorStr, &errorLine,
  14. &errorColumn)) {
  15. QMessageBox::warning(0, QObject::tr("DOM Parser"),
  16. QObject::tr("Parse error at line %1, "
  17. "column %2:\n%3")
  18. .arg(errorLine)
  19. .arg(errorColumn)
  20. .arg(errorStr));
  21. return;
  22. }
  23.  
  24. QDomElement root = doc.documentElement();
  25. if (root.tagName() != listOfNodes.at(0))
  26. return;
  27. QDomNode node = root.firstChild();
  28.  
  29. while (!node.isNull())
  30. {
  31. nodeName = node.toElement().tagName();
  32. QDomElement elem = node.toElement();
  33. if(listOfNodes.contains(nodeName))
  34. {
  35. foreach(QString att, listOfAttrbs)
  36. {
  37. if(elem.hasAttribute(att))
  38. {
  39. nodeName.append(elem.attribute(att,"not set"));
  40. item->setText(column, nodeName);
  41. dbug<<item->text(column) ;
  42. items << item;
  43. column++;
  44. item->setText(column, trim(elem.text()));
  45. dbug<<item->text(column) ;
  46. items << item;
  47. column--;
  48. }
  49. else
  50. {
  51. item->setText(column, nodeName);
  52. dbug<<item->text(column) ;
  53. items << item;
  54. column++;
  55. item->setText(column, trim(elem.text()));
  56. dbug<<item->text(column) ;
  57. items << item;
  58. column--;
  59. }
  60. }
  61. }
  62. node = node.nextSibling();
  63. }
  64. }
To copy to clipboard, switch view to plain text mode 

Code for testing purposes:-
mainwindow.h
Qt Code:
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5.  
  6. #include "domparser.h"
  7. #include <utilities.h>
  8. #include <utils.h>
  9.  
  10. const QStringList nodeList = QStringList()
  11. << "cdData" << "disc_id" << "title" << "artist"
  12. << "year" << "category" << "playlength"
  13. << "noftracks" << "tracktitle" << "tracklength";
  14. const QStringList attrbList = QStringList()
  15. << "no";
  16.  
  17. namespace Ui {
  18. class MainWindow;
  19. }
  20.  
  21. class MainWindow : public QMainWindow
  22. {
  23. Q_OBJECT
  24.  
  25. public:
  26. explicit MainWindow(QWidget *parent = 0);
  27. ~MainWindow();
  28.  
  29. void parseFile(const QString &fileName);
  30.  
  31.  
  32.  
  33. private:
  34. Ui::MainWindow *ui;
  35.  
  36. void setup();
  37.  
  38. QHash<QString, QString> hash;
  39. QList<QTreeWidgetItem *> items;
  40.  
  41.  
  42.  
  43. private slots:
  44. void action(void);
  45. };
  46.  
  47. #endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode 
mainwindow.cpp
Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent) :
  5. QMainWindow(parent),
  6. ui(new Ui::MainWindow)
  7. {
  8. ui->setupUi(this);
  9. setup();
  10. }
  11.  
  12. MainWindow::~MainWindow()
  13. {
  14. delete ui;
  15. }
  16.  
  17. void MainWindow::parseFile(const QString &fileName)
  18. {
  19. QFile file(fileName);
  20. int row = 0, colm = 0;
  21.  
  22. domParser parser(&file, nodeList, attrbList);
  23. for(int i = 0 ; i < parser.items.count() ; ++i)
  24. {
  25. item = parser.items.at(i);
  26. colm = i % 2;
  27. dbug<<item->text(colm); //// This is always the last item in the list!!!
  28. ui->treeWidget->setCurrentItem(item, colm); /// Display is empty
  29. }
  30. }
  31.  
  32. void MainWindow::setup()
  33. {
  34. QStringList headers;
  35.  
  36. headers << "List of data" << "Values";
  37. connect(ui->btn_quit, SIGNAL(clicked()), this, SLOT(close()));
  38. connect(ui->btn_action, SIGNAL(clicked()), this, SLOT(action()));
  39.  
  40. ui->treeWidget->setHeaderLabels(headers);
  41. }
  42.  
  43. void MainWindow::action()
  44. {
  45. QString xmlFile = "/tmp/cddb.xml";
  46. QFile file(xmlFile);
  47. if(!file.exists())
  48. {
  49. infoWarnError(CRITICAL, nice("File action",Black),nice(QString("%1 NOT FOUND!").arg(xmlFile),Red));
  50. return;
  51. }
  52. parseFile(xmlFile);
  53. }
To copy to clipboard, switch view to plain text mode 
main.cpp
Qt Code:
  1. #include "mainwindow.h"
  2. #include <QApplication>
  3. #include <QtWidgets>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. QApplication a(argc, argv);
  8. MainWindow w;
  9. QRect r = w.geometry();
  10. r.moveCenter(QDesktopWidget().availableGeometry().center());
  11. w.setGeometry(r);
  12. w.show();
  13.  
  14. return a.exec();
  15. }
To copy to clipboard, switch view to plain text mode 

This is the xml file I'm reading:-
Qt Code:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <cdData>
  3. <disc_id>
  4. 6411d008
  5. </disc_id>
  6. <title>
  7. Brahms
  8. </title>
  9. <artist>
  10. Symphony No. I
  11. </artist>
  12. <year>
  13. 2000
  14. </year>
  15. <category>
  16. Classical
  17. </category>
  18. <playlength>
  19. 76:00
  20. </playlength>
  21. <noftracks>
  22. 8
  23. </noftracks>
  24. <tracktitle no="01">
  25. Symphony No1. Op.68 Un poco sostenuto
  26. </tracktitle>
  27. <tracklength no="01">
  28. 02:59
  29. </tracklength>
  30. <tracktitle no="02">
  31. Symphony No1. Op.68 Allegro
  32. </tracktitle>
  33. <tracklength no="02">
  34. 10:50
  35. </tracklength>
  36. <tracktitle no="03">
  37. Symphony No1. Op.68 Andante sostenuto
  38. </tracktitle>
  39. <tracklength no="03">
  40. 08:59
  41. </tracklength>
  42. <tracktitle no="04">
  43. Symphony No1. Op.68 Un poco Allegretto e grazioso
  44. </tracktitle>
  45. <tracklength no="04">
  46. 04:48
  47. </tracklength>
  48. <tracktitle no="05">
  49. Symphony No1. Op.68 Adagio-Piu andante
  50. </tracktitle>
  51. <tracklength no="05">
  52. 04:33
  53. </tracklength>
  54. <tracktitle no="06">
  55. Symphony No1. Op.68 Allegro non troppo, ma con brio
  56. </tracktitle>
  57. <tracklength no="06">
  58. 12:26
  59. </tracklength>
  60. <tracktitle no="07">
  61. Variations on a Theme by Haydn, Op56a
  62. </tracktitle>
  63. <tracklength no="07">
  64. 18:43
  65. </tracklength>
  66. <tracktitle no="08">
  67. TragicOverture, Op.81
  68. </tracktitle>
  69. <tracklength no="08">
  70. 12:37
  71. </tracklength>
  72. </cdData>
To copy to clipboard, switch view to plain text mode 

This is the output:-
[ domParser ; 54 ] "disc_id"
[ domParser ; 58 ] "6411d008"
[ domParser ; 54 ] "title"
[ domParser ; 58 ] "Brahms"
[ domParser ; 54 ] "artist"
[ domParser ; 58 ] "Symphony No. I"
[ domParser ; 54 ] "year"
[ domParser ; 58 ] "2000"
[ domParser ; 54 ] "category"
[ domParser ; 58 ] "Classical"
[ domParser ; 54 ] "playlength"
[ domParser ; 58 ] "76:00"
[ domParser ; 54 ] "noftracks"
[ domParser ; 58 ] "8"
[ domParser ; 43 ] "tracktitle01"
[ domParser ; 47 ] "Symphony No1. Op.68 Un poco sostenuto"
[ domParser ; 43 ] "tracklength01"
[ domParser ; 47 ] "02:59"
[ domParser ; 43 ] "tracktitle02"
[ domParser ; 47 ] "Symphony No1. Op.68 Allegro"
[ domParser ; 43 ] "tracklength02"
[ domParser ; 47 ] "10:50"
[ domParser ; 43 ] "tracktitle03"
[ domParser ; 47 ] "Symphony No1. Op.68 Andante sostenuto"
[ domParser ; 43 ] "tracklength03"
[ domParser ; 47 ] "08:59"
[ domParser ; 43 ] "tracktitle04"
[ domParser ; 47 ] "Symphony No1. Op.68 Un poco Allegretto e grazioso"
[ domParser ; 43 ] "tracklength04"
[ domParser ; 47 ] "04:48"
[ domParser ; 43 ] "tracktitle05"
[ domParser ; 47 ] "Symphony No1. Op.68 Adagio-Piu andante"
[ domParser ; 43 ] "tracklength05"
[ domParser ; 47 ] "04:33"
[ domParser ; 43 ] "tracktitle06"
[ domParser ; 47 ] "Symphony No1. Op.68 Allegro non troppo, ma con brio"
[ domParser ; 43 ] "tracklength06"
[ domParser ; 47 ] "12:26"
[ domParser ; 43 ] "tracktitle07"
[ domParser ; 47 ] "Variations on a Theme by Haydn, Op56a"
[ domParser ; 43 ] "tracklength07"
[ domParser ; 47 ] "18:43"
[ domParser ; 43 ] "tracktitle08"
[ domParser ; 47 ] "TragicOverture, Op.81"
[ domParser ; 43 ] "tracklength08"
[ domParser ; 47 ] "12:37"
[ parseFile ; 28 ] "tracklength08"
[ parseFile ; 28 ] "12:37"
[ parseFile ; 28 ] "tracklength08"
[ parseFile ; 28 ] "12:37"
[ parseFile ; 28 ] "tracklength08"
[ parseFile ; 28 ] "12:37"
[ parseFile ; 28 ] "tracklength08"
[ parseFile ; 28 ] "12:37"
[ parseFile ; 28 ] "tracklength08"
[ parseFile ; 28 ] "12:37"
[ parseFile ; 28 ] "tracklength08"
[ parseFile ; 28 ] "12:37"
[ parseFile ; 28 ] "tracklength08"
[ parseFile ; 28 ] "12:37"
[ parseFile ; 28 ] "tracklength08"
[ parseFile ; 28 ] "12:37"
[ parseFile ; 28 ] "tracklength08"
[ parseFile ; 28 ] "12:37"
[ parseFile ; 28 ] "tracklength08"
[ parseFile ; 28 ] "12:37"
[ parseFile ; 28 ] "tracklength08"
[ parseFile ; 28 ] "12:37"
[ parseFile ; 28 ] "tracklength08"
[ parseFile ; 28 ] "12:37"
[ parseFile ; 28 ] "tracklength08"
[ parseFile ; 28 ] "12:37"
[ parseFile ; 28 ] "tracklength08"
[ parseFile ; 28 ] "12:37"
[ parseFile ; 28 ] "tracklength08"
[ parseFile ; 28 ] "12:37"
[ parseFile ; 28 ] "tracklength08"
[ parseFile ; 28 ] "12:37" etc....
I appreciate any help, thank you
Graham