首先是向tableview中添加内容
model=new QStandardItemModel(); model->setHorizontalHeaderItem(0, new QStandardItem(QObject::tr("id"))); //model->setHorizontalHeaderItem(1, new QStandardItem(QObject::tr("场馆编号"))); model->setHorizontalHeaderItem(1, new QStandardItem(QObject::tr("昵称"))); //QListtmplist; //model->insertRow(0,tmplist); //ui->order->setModel(model); ui->tableView->setModel(model); ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows); QSqlQuery query; QString sql="select * from tuser"; if(!query.exec(sql)){ QMessageBox::warning(this,"error","连接失败"); this->close(); } QList list; int t=0; while(query.next()) { qDebug()< setItem(t,0,id); model->setItem(t++,1,name); }
然后是获取某一选中行的信息
int row=ui->tableView->currentIndex().row(); if(row<0){ QMessageBox::warning(this,"error","请先选中某条订单",QMessageBox::Yes); return; } //弹出确认删除信息消息框 QMessageBox *del; QString mes=""; mes=model->item(row,0)->data(Qt::DisplayRole).toString(); mes=value+model->item(row,1)->data(Qt::DisplayRole).toString();
获取cell中的内容
QStandardItem * QStandardItemModel::item ( int row, int column = 0 ) constmodel->item(row, column)->text()就得到了文本内容。QTableWidget 约等于 QTableView + Model