PDA

View Full Version : QTableWidget problems



eleanor
6th May 2009, 00: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?

spirit
6th May 2009, 07:31
how do you set central widget? can we see this code?

eleanor
6th May 2009, 10:39
Yes, you may, like this:



/* window preferences */
this->resize(800,600);
this->setWindowTitle("Test");

parser = new DOMParser();
setCentralWidget(parser);

create_actions();
create_menus();
create_toolbars();

spirit
6th May 2009, 10:46
whe do you pass QIODevice in your code to DOMParser::read(QIODevice *device)?
use qDebug in DOMParser::read(QIODevice *device) and you'll see when it is called if it's called at all. :)

eleanor
6th May 2009, 10:58
It is called...when I go under file->open and then choose an xml file...that read() function is called sucessfully...but there's still nothing to be seen on the screen.

spirit
6th May 2009, 11:00
I don't see where you increment rowCount and columnCount.
do you set rowCount & columnCount?

lyuts
22nd May 2009, 14:18
What is the base class for DOMParser?