eleanor
5th May 2009, 23:57
Hi, what I want to do is create a excel like editable table where I would store XML parsed data...
I created a class like this:
DOMParser::DOMParser(QWidget *parent) : QTableWidget(parent) {
}
bool DOMParser::read(QIODevice *device) {
/* column names */
QStringList labels;
/* error variables for DOM document */
QString error_str;
int error_line;
int error_column;
/* parse the DOM document == true if successful */
if(!dom_document.setContent(device, true, &error_str, &error_line, &error_column)) {
QMessageBox::information(window(),
tr("DOM Reader"),
tr("Parse error at line %1, column %2:\n%3")
.arg(error_line)
.arg(error_column)
.arg(error_str));
return false;
}
/* root element */
QDomElement root = dom_document.documentElement();
qDebug() << "ROOT: " << root.tagName() << endl;
QDomElement child = root.firstChildElement();
while(!child.isNull()) {
parse_element(child);
child = child.nextSiblingElement();
}
return true;
}
void DOMParser::parse_element(const QDomElement &element, QTableWidgetItem *parent_item) {
qDebug() << "Element: " << element.tagName();
QTableWidgetItem *item = new QTableWidgetItem();
QDomNode child = element.firstChild();
if(child.hasChildNodes()) {
QString value = element.firstChildElement().text();
item->setText(value);
this->setItem(this->rowCount(), this->columnCount(), item);
}
while(!child.isNull()) {
parse_element(child.toElement(), item);
child = child.nextSiblingElement();
}
}
But the problem is that when I set centralWidget(new DOMParser) in another class, there's nothing displayed on the screen. What am I doing wrong?
I created a class like this:
DOMParser::DOMParser(QWidget *parent) : QTableWidget(parent) {
}
bool DOMParser::read(QIODevice *device) {
/* column names */
QStringList labels;
/* error variables for DOM document */
QString error_str;
int error_line;
int error_column;
/* parse the DOM document == true if successful */
if(!dom_document.setContent(device, true, &error_str, &error_line, &error_column)) {
QMessageBox::information(window(),
tr("DOM Reader"),
tr("Parse error at line %1, column %2:\n%3")
.arg(error_line)
.arg(error_column)
.arg(error_str));
return false;
}
/* root element */
QDomElement root = dom_document.documentElement();
qDebug() << "ROOT: " << root.tagName() << endl;
QDomElement child = root.firstChildElement();
while(!child.isNull()) {
parse_element(child);
child = child.nextSiblingElement();
}
return true;
}
void DOMParser::parse_element(const QDomElement &element, QTableWidgetItem *parent_item) {
qDebug() << "Element: " << element.tagName();
QTableWidgetItem *item = new QTableWidgetItem();
QDomNode child = element.firstChild();
if(child.hasChildNodes()) {
QString value = element.firstChildElement().text();
item->setText(value);
this->setItem(this->rowCount(), this->columnCount(), item);
}
while(!child.isNull()) {
parse_element(child.toElement(), item);
child = child.nextSiblingElement();
}
}
But the problem is that when I set centralWidget(new DOMParser) in another class, there's nothing displayed on the screen. What am I doing wrong?