PDA

View Full Version : QTableWidgetItem::setData crashes



RubyTigerSG
10th April 2011, 03:15
Hi Gurus,

Can anybody tell me what is wrong with this code?


QHash<QString, double> results = ReturnResults();
int row = 0;
QHashIterator<QString, double> i(results);
while (i.hasNext()) {
i.next();
QVariant key;
key = i.key();
QTableWidgetItem *newLine = tblResult->item(row,0);
newLine->setData(Qt::UserRole,key);


cout << i.key() << ": " << i.value() << endl;
row++;
}

Any help will be much appreciated.

RT

ChiliPalmer
10th April 2011, 13:21
The QTableWidgetItem has to be created by hand, so the first question would be wether you did that. Otherwise QTableWidget::item returns 0.
If the code you posted is supposed to create the table, it would have to look like this:


QHash<QString, double> results = ReturnResults();
tblResult->setRowCount(results.size());
int row = 0;
QHashIterator<QString, double> i(results);
while (i.hasNext()) {
i.next();
QVariant key;
key = i.key();
QTableWidgetItem *newLine = new QTableWidgetItem();
newLine->setData(Qt::UserRole,key);
tblResult->setItem(row, 0, newLine);

cout << i.key() << ": " << i.value() << endl;
row++;
}