Reputation: 33823
I have already an empty QTableWidget
, and I want to add items to it one by one.
I did the following code:
void Widget::on_add_btn_clicked(){
ui->tableWidget->insertRow(ui->tableWidget->rowCount());
ui->tableWidget->setItem(ui->tableWidget->rowCount(), 1, new QTableWidgetItem("Hello"));
}
The result of the previous code is adds a new empty row without text.
How to add a new row with its text (not empty)?
Upvotes: 0
Views: 207
Reputation: 969
The row count is equal to one, but the index of the row that you are trying to populate is 0. Try to change
ui->tableWidget->setItem(ui->tableWidget->rowCount(), 1, new QTableWidgetItem("Hello"));
to
ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1, 1, new QTableWidgetItem("Hello"));
It is also possible that you forgot about inserting columns. In your case you should insert at least two columns, because first column will be indexed with 0, second with 1. You can do it using insertColumn method.
Upvotes: 1