tags:

views:

180

answers:

2

I've been trying to use QT4 with a QTableWidget to store data. I seem to be unable to select a cell and get the text out of it and wanted to see why it won't retrieve it. ui->QTableWidget->item(ui->QTableWidget->rowCount(),0)->setText("");

+2  A: 

QTableWidget uses indices which are zero based, so qTableWidget->rowCount() is one past the end of your table.

To iterate over your items and see their text, you could do something like this:

// assuming #include <QtDebug>
for (int i=0; i<tableWidget->rowCount(); ++i)
{
    qDebug() << tableWidget->item(i, 0)->text();
}
Kaleb Pederson
executing this causes the program to crash:qDebug()<<ui->tableWidget->item(0,1)->text();
eyecreate
Have you added anything to the list? If not, item(0,1) will return NULL and the attempt to dereference the NULL pointer to get the text will result in a crash.
Kaleb Pederson
How do I add something to the list? I've been creating a row that's it, I though the row would have blank cells when created. How do I assign values into those new cells when inserting a row?
eyecreate
@eyecreate, when you create a row it adds that row to the count, but it doesn't add any items for the row. You'll need to do that yourself.
Caleb Huitt - cjhuitt
A: 

It seems I didn't realize that I had to make a new Item object for each cell. I solved this by initializing it "empty"

ui->tablewidget->setItem(ui->tablewidget->rowCount()-1,0,new QTableWidgetItem(""));
eyecreate