需要先设置好行数
// 先设置好表格的总行数
int rowsize = 10;
ui->tableWidget->setRowCount(rowsize);//总行数
然后就可以正常添加单元格了
for(int row=0; row<ui->tableWidget->rowCount(); row++)
{
QTableWidgetItem *item_a= new QTableWidgetItem ("a");QTableWidgetItem *item_b = new QTableWidgetItem ("b");QTableWidgetItem *item_c = new QTableWidgetItem ("c");QTableWidgetItem *item_d = new QTableWidgetItem ("d");QTableWidgetItem *item_e = new QTableWidgetItem ("e");QTableWidgetItem *item_f = new QTableWidgetItem ("f");// 添加单元格ui->tableWidget_right->setItem(row,0,item_a);ui->tableWidget_right->setItem(row,1,item_b);ui->tableWidget_right->setItem(row,2,item_c);ui->tableWidget_right->setItem(row,3,item_d );ui->tableWidget_right->setItem(row,4,item_e );ui->tableWidget_right->setItem(row,5,item_f);}