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
2010-02-04 21:26:10
executing this causes the program to crash:qDebug()<<ui->tableWidget->item(0,1)->text();
eyecreate
2010-02-04 23:29:44
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
2010-02-05 01:13:44
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
2010-02-05 02:30:00
@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
2010-02-05 16:52:25
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
2010-02-05 03:53:14