墨香年少 32 发布于 2021年11月4日 添加一行 int i = ui->tableWidget->rowCount(); ui->tableWidget->insertRow(i); ui->tableWidget->setItem(i,0,new QTableWidgetItem("AAA"+QString::number(i))); ui->tableWidget->setItem(i,1,new QTableWidgetItem("BBB")); ui->tableWidget->setItem(i,2,new QTableWidgetItem("CCC")); ui->tableWidget->setItem(i,3,new QTableWidgetItem("DDD")); 删除一行 QTableWidgetItem* item = ui->tableWidget->item(i,0); if("AAA5" == item->text()) { ui->tableWidget->removeRow(i); } 下面是我的方法代码: //点击了toolbar void ServerMain::action_triggered(QAction * _action) { QString text = _action->text(); if("开启服务" == text) { this->startService(); } else if("测试添加" == text) { int i = ui->tableWidget->rowCount(); ui->tableWidget->insertRow(i); ui->tableWidget->setItem(i,0,new QTableWidgetItem("AAA"+QString::number(i))); ui->tableWidget->setItem(i,1,new QTableWidgetItem("BBB")); ui->tableWidget->setItem(i,2,new QTableWidgetItem("CCC")); ui->tableWidget->setItem(i,3,new QTableWidgetItem("DDD")); }else if("测试删除" == text){ for (int i = 0; i < ui->tableWidget->rowCount(); i++) { QTableWidgetItem* item = ui->tableWidget->item(i,0); if("AAA5" == item->text()) { ui->tableWidget->removeRow(i); } } } } 目之所及,皆是回忆,心之所想,皆是过往 分享这篇帖子 链接帖子 分享到其他站点