首先是向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("昵称"))); //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=0; while(query.next()) { qDebug()<<query.value(0).toString(); QStandardItem *id=new QStandardItem(query.value(0).toString()); QStandardItem *name=new QStandardItem(query.value(1).toString()); model->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 ) const model->item(row, column)->text()就得到了文本内容。 QTableWidget 约等于 QTableView + Model