0

I would like to obtain a cell in a QTableWidget.

I have tried with the method "item", but it returns 0, even if my table has 1 row and 1 column and I call the method like this :

QTableWidgetItem *it = ui->tableWidget->item(0, 0);
4

2 回答 2

1

当您通过设计器将文本添加到单元格时,它会自动将项目设置为单元格。虽然以编程方式,您必须先创建然后设置项目:

QTableWidgetItem *item = new QTableWidgetItem("text");
ui->tableWidget->setItem(0, 0, item);

所以首先你应该通过设置项目(甚至是空的)来初始化你的表。

于 2013-06-26T09:28:27.153 回答
0

TableWidget 不会返回超出列/行数的项目

设置行数和列数:

void setRowCount(int rows)
void setColumnCount(int columns)

ui->tableWidget->setRowCount(1);
ui->tableWidget->setColumnCount(1);
于 2013-06-25T10:03:29.923 回答