开发者

retrieve the text in a specific cell in a QTableWidget?

开发者 https://www.devze.com 2022-12-18 23:42 出处:网络
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.

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(""));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号