All Classes | Qt 5.8
http://doc.qt.io/qt-5/classes.html
ui->tableWidget->setColumnCount(2);
ui->tableWidget->setRowCount(2);
ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "sno" << "sname");
//选择的表现方式,整行或者整列
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
//是否可以编辑,需双击单元格
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
//选择的模式,单行或多行
ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
ui->tableWidget->verticalHeader()->setVisible(false);
//添加记录
ui->tableWidget->setItem(0, 0, new QTableWidgetItem("0811101"));
ui->tableWidget->setItem(0, 1, new QTableWidgetItem(tr("李勇")));
ui->tableWidget->setItem(1, 0, new QTableWidgetItem("0811102"));
ui->tableWidget->setItem(1, 1, new QTableWidgetItem(tr("刘晨")));
//动态添加
int cols = ui->tableWidget->columnCount();
int rows = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(rows);
for (int i = 0; i < cols; ++i)
{
ui->tableWidget->setItem(rows, i, new QTableWidgetItem("new" + QString::number(rows)));
}
ui->tableWidget->selectRow(rows);
//删除记录
QTableWidgetItem *item = ui->tableWidget->currentItem();
if (item != nullptr)
{
ui->tableWidget->removeRow(item->row());
}
//修改记录
QList<QTableWidgetItem *> listItem = ui->tableWidget->selectedItems();
if (listItem.count() != 0)
{
foreach (QTableWidgetItem *item, listItem)
{
item->setText("modify");
}
}