做数据库的话 QtableWidget 控件 应该用得很多的,做个记录
//QtableWidget 控件 //加水平表头 QStringList list1; list1<<"姓名"<<"性别"<<"年龄"; ui->tableWidget->setColumnCount(list1.size()); ui->tableWidget->setHorizontalHeaderLabels(list1); ui->tableWidget->setRowCount(10); //设行数 ui->tableWidget->setItem(0,0,new QTableWidgetItem("jinesc")); QStringList nameList; nameList<<"kiki"<<"jinesc"<<"bbb"; QList<QString> sexList; sexList<<"女"<<"男"<<"未知"; for (int i=0;i<3;i++) { int col=0; ui->tableWidget->setItem(i,col++,new QTableWidgetItem(nameList[i])); ui->tableWidget->setItem(i,col++,new QTableWidgetItem(sexList.at(i))); ui->tableWidget->setItem(i,col++,new QTableWidgetItem(QString::number(i+20))); } connect(ui->addButton,&QPushButton::clicked,[=](){ bool isEmpty=ui->tableWidget->findItems("jinesc.cn",Qt::MatchExactly).empty(); if(isEmpty){ ui->tableWidget->insertRow(0); int col=0; ui->tableWidget->setItem(0,col++,new QTableWidgetItem("jinesc.cn")); ui->tableWidget->setItem(0,col++,new QTableWidgetItem("网站")); }else{ QMessageBox::warning(this,"警告","已经添加了jinesc.cn"); } }); connect(ui->delButton,&QPushButton::clicked,[=](){ bool isEmpty=ui->tableWidget->findItems("jinesc.cn",Qt::MatchExactly).empty(); if(isEmpty){ QMessageBox::warning(this,"警告","已经删除了jinesc.cn"); }else{ //找到所在行 int row=ui->tableWidget->findItems("jinesc.cn",Qt::MatchExactly).first()->row(); ui->tableWidget->removeRow(row); } }); ui->tableWidget->editItem(ui->tableWidget->item(0,1));//编辑某个单元格
运行如图
发表评论