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->rowCo开发者_C百科unt(),0)->setText("");
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();
}
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(""));
精彩评论