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);
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);
当您通过设计器将文本添加到单元格时,它会自动将项目设置为单元格。虽然以编程方式,您必须先创建然后设置项目:
QTableWidgetItem *item = new QTableWidgetItem("text");
ui->tableWidget->setItem(0, 0, item);
所以首先你应该通过设置项目(甚至是空的)来初始化你的表。
TableWidget 不会返回超出列/行数的项目
设置行数和列数:
void setRowCount(int rows)
void setColumnCount(int columns)
ui->tableWidget->setRowCount(1);
ui->tableWidget->setColumnCount(1);