Question

I have a QTableWidget and I can't get anything to show up in it.

The following appears in the constructor of the main window:

ui->tableWidget->setItem(0,0,new QTableWidgetItem("Item1"));
ui->tableWidget->setItem(0,1,new QTableWidgetItem("Item2"));
ui->tableWidget->setItem(0,2,new QTableWidgetItem("Item3"));

When I run the application, the table widget shows up, but the items do not.

I tried adding ui->tableWidget->insertRow(0); before the above code, but it didn't work.

Was it helpful?

Solution

Aha! I figured out what was going on... I needed to tell the control the number of rows it should have:

ui->tableWidget->setRowCount(2);

OTHER TIPS

Example code:

//this will give the present number of rows available.
int insertRow = ui->tableWidget->rowCount();

//insert the row at the bottom of the table widget - using.
ui->tableWidget->insertRow(insertRow);

//After a new row is inserted we can add the table widget items as required.
ui->tableWidget->setItem(insertRow,0,new QTableWidgetItem("Item1"));
ui->tableWidget->setItem(insertRow,1,new QTableWidgetItem("Item2"));
ui->tableWidget->setItem(insertRow,2,new QTableWidgetItem("Item3"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top