#include "form6.h" #include "ui_form6.h" Form6::Form6(QWidget *parent) : QWidget(parent), ui(new Ui::Form6) { ui->setupUi(this); } Form6::~Form6() { delete ui; } void Form6::on_addBtn_clicked() { // 获取当前行 int maxRow = ui->tableWidget->rowCount(); // 新增一行空行 ui->tableWidget->insertRow(maxRow); QTableWidgetItem *sidItem = new QTableWidgetItem(ui->sidEdit->text()); QTableWidgetItem *nameItem = new QTableWidgetItem(ui->nameEdit->text()); QTableWidgetItem *sexItem = new QTableWidgetItem(ui->sexBox->currentText()); QTableWidgetItem *ageItem = new QTableWidgetItem(ui->ageEdit->text()); ui->tableWidget->setItem(maxRow,0,sidItem); ui->tableWidget->setItem(maxRow,1,nameItem); ui->tableWidget->setItem(maxRow,2,sexItem); ui->tableWidget->setItem(maxRow,3,ageItem); } void Form6::on_getBtn_clicked() { int selectedRow = ui->tableWidget->currentRow(); // int maxCol = ui->tableWidget->columnCount(); QString sid = ui->tableWidget->item(selectedRow,0)->text(); ui->sidEdit->setText(sid); QString name = ui->tableWidget->item(selectedRow,1)->text(); ui->nameEdit->setText(name); QString sex = ui->tableWidget->item(selectedRow,2)->text(); ui->sexBox->setCurrentText(sex); QString age = ui->tableWidget->item(selectedRow,3)->text(); ui->ageEdit->setText(age); } void Form6::on_delBtn_clicked() { int row = ui->tableWidget->currentRow(); ui->tableWidget->removeRow(row); // 删除行 // for(int col = 0; col tableWidget->columnCount();col++) // { // ui->tableWidget->takeItem(row,col); // } }