GrahamB
17th January 2014, 08:48
Sirs
I am having a problem displaying a Qlist of QTreeWidgetItems in a QTreeWidget.
Code follows:-
domparser.h
#ifndef DOMPARSER_H
#define DOMPARSER_H
#include <QTreeWidget>
#include <QtXml/QDomElement>
#include <QTreeWidgetItem>
#include <QMessageBox>
#include <utilities.h>
#include <utils.h>
class domParser
{
public:
domParser(QIODevice *device, QStringList nodeList, QStringList attrbList);
QList<QTreeWidgetItem *> items;
private:
QStringList listOfNodes, listOfAttrbs;
};
#endif // DOMPARSER_H
domparser.cpp
#include "domparser.h"
domParser::domParser(QIODevice *device, QStringList nodeList, QStringList attrbList)
{
listOfNodes = nodeList;
listOfAttrbs = attrbList;
int column = 0;
QTreeWidgetItem *item = new QTreeWidgetItem;
QString errorStr, nodeName;
int errorLine;
int errorColumn;
QDomDocument doc;
if (!doc.setContent(device, true, &errorStr, &errorLine,
&errorColumn)) {
QMessageBox::warning(0, QObject::tr("DOM Parser"),
QObject::tr("Parse error at line %1, "
"column %2:\n%3")
.arg(errorLine)
.arg(errorColumn)
.arg(errorStr));
return;
}
QDomElement root = doc.documentElement();
if (root.tagName() != listOfNodes.at(0))
return;
QDomNode node = root.firstChild();
while (!node.isNull())
{
nodeName = node.toElement().tagName();
QDomElement elem = node.toElement();
if(listOfNodes.contains(nodeName))
{
foreach(QString att, listOfAttrbs)
{
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();
}
}
Code for testing purposes:-
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "domparser.h"
#include <utilities.h>
#include <utils.h>
const QStringList nodeList = QStringList()
<< "cdData" << "disc_id" << "title" << "artist"
<< "year" << "category" << "playlength"
<< "noftracks" << "tracktitle" << "tracklength";
const QStringList attrbList = QStringList()
<< "no";
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void parseFile(const QString &fileName);
private:
Ui::MainWindow *ui;
void setup();
QHash<QString, QString> hash;
QList<QTreeWidgetItem *> items;
QTreeWidgetItem item;
private slots:
void action(void);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setup();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::parseFile(const QString &fileName)
{
QFile file(fileName);
int row = 0, colm = 0;
domParser parser(&file, nodeList, attrbList);
for(int i = 0 ; i < parser.items.count() ; ++i)
{
QTreeWidgetItem *item = new QTreeWidgetItem;
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()
{
QString xmlFile = "/tmp/cddb.xml";
QFile file(xmlFile);
if(!file.exists())
{
infoWarnError(CRITICAL, nice("File action",Black),nice(QString("%1 NOT FOUND!").arg(xmlFile),Red));
return;
}
parseFile(xmlFile);
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QRect r = w.geometry();
r.moveCenter(QDesktopWidget().availableGeometry(). center());
w.setGeometry(r);
w.show();
return a.exec();
}
This is the xml file I'm reading:-
<?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>
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
I am having a problem displaying a Qlist of QTreeWidgetItems in a QTreeWidget.
Code follows:-
domparser.h
#ifndef DOMPARSER_H
#define DOMPARSER_H
#include <QTreeWidget>
#include <QtXml/QDomElement>
#include <QTreeWidgetItem>
#include <QMessageBox>
#include <utilities.h>
#include <utils.h>
class domParser
{
public:
domParser(QIODevice *device, QStringList nodeList, QStringList attrbList);
QList<QTreeWidgetItem *> items;
private:
QStringList listOfNodes, listOfAttrbs;
};
#endif // DOMPARSER_H
domparser.cpp
#include "domparser.h"
domParser::domParser(QIODevice *device, QStringList nodeList, QStringList attrbList)
{
listOfNodes = nodeList;
listOfAttrbs = attrbList;
int column = 0;
QTreeWidgetItem *item = new QTreeWidgetItem;
QString errorStr, nodeName;
int errorLine;
int errorColumn;
QDomDocument doc;
if (!doc.setContent(device, true, &errorStr, &errorLine,
&errorColumn)) {
QMessageBox::warning(0, QObject::tr("DOM Parser"),
QObject::tr("Parse error at line %1, "
"column %2:\n%3")
.arg(errorLine)
.arg(errorColumn)
.arg(errorStr));
return;
}
QDomElement root = doc.documentElement();
if (root.tagName() != listOfNodes.at(0))
return;
QDomNode node = root.firstChild();
while (!node.isNull())
{
nodeName = node.toElement().tagName();
QDomElement elem = node.toElement();
if(listOfNodes.contains(nodeName))
{
foreach(QString att, listOfAttrbs)
{
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();
}
}
Code for testing purposes:-
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "domparser.h"
#include <utilities.h>
#include <utils.h>
const QStringList nodeList = QStringList()
<< "cdData" << "disc_id" << "title" << "artist"
<< "year" << "category" << "playlength"
<< "noftracks" << "tracktitle" << "tracklength";
const QStringList attrbList = QStringList()
<< "no";
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void parseFile(const QString &fileName);
private:
Ui::MainWindow *ui;
void setup();
QHash<QString, QString> hash;
QList<QTreeWidgetItem *> items;
QTreeWidgetItem item;
private slots:
void action(void);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setup();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::parseFile(const QString &fileName)
{
QFile file(fileName);
int row = 0, colm = 0;
domParser parser(&file, nodeList, attrbList);
for(int i = 0 ; i < parser.items.count() ; ++i)
{
QTreeWidgetItem *item = new QTreeWidgetItem;
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()
{
QString xmlFile = "/tmp/cddb.xml";
QFile file(xmlFile);
if(!file.exists())
{
infoWarnError(CRITICAL, nice("File action",Black),nice(QString("%1 NOT FOUND!").arg(xmlFile),Red));
return;
}
parseFile(xmlFile);
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QRect r = w.geometry();
r.moveCenter(QDesktopWidget().availableGeometry(). center());
w.setGeometry(r);
w.show();
return a.exec();
}
This is the xml file I'm reading:-
<?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>
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