首先是向tableview中添加内容
model=new QStandardItemModel();
model->setHorizontalHeaderItem(, new QStandardItem(QObject::tr("id")));
//model->setHorizontalHeaderItem(1, new QStandardItem(QObject::tr("场馆编号")));
model->setHorizontalHeaderItem(, new QStandardItem(QObject::tr("昵称"))); //QList<QStandardItem*> tmplist;
//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<QStandardItem*> list;
int t=;
while(query.next())
{
qDebug()<<query.value().toString();
QStandardItem *id=new QStandardItem(query.value().toString());
QStandardItem *name=new QStandardItem(query.value().toString());
model->setItem(t,,id);
model->setItem(t++,,name); }
然后是获取某一选中行的信息
int row=ui->tableView->currentIndex().row();
if(row<){
QMessageBox::warning(this,"error","请先选中某条订单",QMessageBox::Yes);
return;
}
//弹出确认删除信息消息框
QMessageBox *del;
QString mes="";
mes=model->item(row,)->data(Qt::DisplayRole).toString();
mes=value+model->item(row,)->data(Qt::DisplayRole).toString();
获取cell中的内容
QStandardItem * QStandardItemModel::item ( int row, int column = ) const model->item(row, column)->text()就得到了文本内容。 QTableWidget 约等于 QTableView + Model