Sirs
I am having a problem displaying a Qlist of QTreeWidgetItems in a QTreeWidget.
Code follows:-
domparser.h
Qt Code:
#ifndef DOMPARSER_H #define DOMPARSER_H #include <QTreeWidget> #include <QtXml/QDomElement> #include <QTreeWidgetItem> #include <QMessageBox> #include <utilities.h> #include <utils.h> class domParser { public: QList<QTreeWidgetItem *> items; private: QStringList listOfNodes, listOfAttrbs; }; #endif // DOMPARSER_HTo copy to clipboard, switch view to plain text mode
domparser.cpp
Qt Code:
#include "domparser.h" { listOfNodes = nodeList; listOfAttrbs = attrbList; int column = 0; QString errorStr, nodeName; int errorLine; int errorColumn; QDomDocument doc; if (!doc.setContent(device, true, &errorStr, &errorLine, &errorColumn)) { "column %2:\n%3") .arg(errorLine) .arg(errorColumn) .arg(errorStr)); return; } if (root.tagName() != listOfNodes.at(0)) return; while (!node.isNull()) { nodeName = node.toElement().tagName(); if(listOfNodes.contains(nodeName)) { { if(elem.hasAttribute(att)) { nodeName.append(elem.attribute(att,"not set")); item->setText(column, nodeName); dbug<<item->text(column) ; items << item; column++; item->setText(column, trim(elem.text())); dbug<<item->text(column) ; items << item; column--; } else { item->setText(column, nodeName); dbug<<item->text(column) ; items << item; column++; item->setText(column, trim(elem.text())); dbug<<item->text(column) ; items << item; column--; } } } node = node.nextSibling(); } }To copy to clipboard, switch view to plain text mode
Code for testing purposes:-
mainwindow.h
mainwindow.cppQt Code:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include "domparser.h" #include <utilities.h> #include <utils.h> << "cdData" << "disc_id" << "title" << "artist" << "year" << "category" << "playlength" << "noftracks" << "tracktitle" << "tracklength"; << "no"; namespace Ui { class MainWindow; } { Q_OBJECT public: ~MainWindow(); private: Ui::MainWindow *ui; void setup(); QHash<QString, QString> hash; QList<QTreeWidgetItem *> items; QTreeWidgetItem item; private slots: void action(void); }; #endif // MAINWINDOW_HTo copy to clipboard, switch view to plain text mode
main.cppQt Code:
#include "mainwindow.h" #include "ui_mainwindow.h" ui(new Ui::MainWindow) { ui->setupUi(this); setup(); } MainWindow::~MainWindow() { delete ui; } { int row = 0, colm = 0; domParser parser(&file, nodeList, attrbList); for(int i = 0 ; i < parser.items.count() ; ++i) { item = parser.items.at(i); colm = i % 2; dbug<<item->text(colm); //// This is always the last item in the list!!! ui->treeWidget->setCurrentItem(item, colm); /// Display is empty } } void MainWindow::setup() { QStringList headers; headers << "List of data" << "Values"; connect(ui->btn_quit, SIGNAL(clicked()), this, SLOT(close())); connect(ui->btn_action, SIGNAL(clicked()), this, SLOT(action())); ui->treeWidget->setHeaderLabels(headers); } void MainWindow::action() { if(!file.exists()) { return; } parseFile(xmlFile); }To copy to clipboard, switch view to plain text mode
Qt Code:
#include "mainwindow.h" #include <QApplication> #include <QtWidgets> int main(int argc, char *argv[]) { MainWindow w; w.setGeometry(r); w.show(); return a.exec(); }To copy to clipboard, switch view to plain text mode
This is the xml file I'm reading:-
Qt Code:
<?xml version="1.0" encoding="UTF-8"?> <cdData> <disc_id> 6411d008 </disc_id> <title> Brahms </title> <artist> Symphony No. I </artist> <year> 2000 </year> <category> Classical </category> <playlength> 76:00 </playlength> <noftracks> 8 </noftracks> <tracktitle no="01"> Symphony No1. Op.68 Un poco sostenuto </tracktitle> <tracklength no="01"> 02:59 </tracklength> <tracktitle no="02"> Symphony No1. Op.68 Allegro </tracktitle> <tracklength no="02"> 10:50 </tracklength> <tracktitle no="03"> Symphony No1. Op.68 Andante sostenuto </tracktitle> <tracklength no="03"> 08:59 </tracklength> <tracktitle no="04"> Symphony No1. Op.68 Un poco Allegretto e grazioso </tracktitle> <tracklength no="04"> 04:48 </tracklength> <tracktitle no="05"> Symphony No1. Op.68 Adagio-Piu andante </tracktitle> <tracklength no="05"> 04:33 </tracklength> <tracktitle no="06"> Symphony No1. Op.68 Allegro non troppo, ma con brio </tracktitle> <tracklength no="06"> 12:26 </tracklength> <tracktitle no="07"> Variations on a Theme by Haydn, Op56a </tracktitle> <tracklength no="07"> 18:43 </tracklength> <tracktitle no="08"> TragicOverture, Op.81 </tracktitle> <tracklength no="08"> 12:37 </tracklength> </cdData>To copy to clipboard, switch view to plain text mode
This is the output:-
I appreciate any help, thank you[ 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....
Graham
Bookmarks