一、效果展示
二、实现方法
1、先对tablewidget设置
this->ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
this->ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //单击选择行
this->ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection); //设置只能选择一行
this->ui->tableWidget->setEditTriggers(QAbstractItemView::DoubleClicked); //设置内容不可更改
this->ui->tableWidget->setAlternatingRowColors(true); //设置隔行改变颜色
this->ui->tableWidget->verticalHeader()->setVisible(true); //显示行号
this->ui->tableWidget->setTextElideMode(Qt::ElideMiddle);
this->ui->tableWidget->setColumnCount(peopleItemMax);
this->ui->tableWidget->setRowCount(0);
this->ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<tr("人员名称")<<tr("等待时间(分钟)")<<tr("开始时间")<<tr("结束时间"));
this->ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
this->ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
2、实现行的上下移动
主要实现方式是通过交换两行的数据来实现的文章来源:https://www.toymoban.com/news/detail-503904.html
QString currItemNumber = ui->tableWidget->item(currSelectRow,orderItemNumber)->text();//合同编号
QString currItemProductName = ui->tableWidget->item(currSelectRow,orderItemProductName)->text();//零件名称
QString currItemProductNo = ui->tableWidget->item(currSelectRow,orderItemProductNo)->text();//零件编号
QString currItemDrawingNo = ui->tableWidget->item(currSelectRow,orderItemDrawingNo)->text();//图纸图号
QString currItemMaterial = ui->tableWidget->item(currSelectRow,orderItemMaterial)->text();//材质
QString currItemQuantity = ui->tableWidget->item(currSelectRow,orderItemQuantity)->text();//数量
QString currItemWorkTime = ui->tableWidget->item(currSelectRow,orderItemWorkTime)->text();; //工作时间
QDate currItemDeliveryDate= ((QDateEdit *)ui->tableWidget->cellWidget(currSelectRow,orderItemDeliveryDate))->date(); //交期
QString currItemRemarks = ui->tableWidget->item(currSelectRow,orderItemRemarks)->text();//备注
ui->tableWidget->item(currSelectRow,orderItemNumber)->setText(ui->tableWidget->item(moveRow,orderItemNumber)->text());//合同编号
ui->tableWidget->item(currSelectRow,orderItemProductName)->setText(ui->tableWidget->item(moveRow,orderItemProductName)->text());//零件名称
ui->tableWidget->item(currSelectRow,orderItemProductNo)->setText(ui->tableWidget->item(moveRow,orderItemProductNo)->text());//零件编号
ui->tableWidget->item(currSelectRow,orderItemDrawingNo)->setText(ui->tableWidget->item(moveRow,orderItemDrawingNo)->text());//图纸编号
ui->tableWidget->item(currSelectRow,orderItemMaterial)->setText(ui->tableWidget->item(moveRow,orderItemMaterial)->text());//材质
ui->tableWidget->item(currSelectRow,orderItemQuantity)->setText(ui->tableWidget->item(moveRow,orderItemQuantity)->text());//数量
ui->tableWidget->item(currSelectRow,orderItemWorkTime)->setText(ui->tableWidget->item(moveRow,orderItemWorkTime)->text());//工作时间
((QDateEdit *)ui->tableWidget->cellWidget(currSelectRow,orderItemDeliveryDate))//交期
->setDate(((QDateEdit *)ui->tableWidget->cellWidget(moveRow,orderItemDeliveryDate))->date());
ui->tableWidget->item(currSelectRow,orderItemRemarks)->setText(ui->tableWidget->item(moveRow,orderItemRemarks)->text());//备注
ui->tableWidget->item(moveRow,orderItemNumber)->setText(currItemNumber);//合同编号
ui->tableWidget->item(moveRow,orderItemProductName)->setText(currItemProductName);//零件名称
ui->tableWidget->item(moveRow,orderItemProductNo)->setText(currItemProductNo);//零件编号
ui->tableWidget->item(moveRow,orderItemDrawingNo)->setText(currItemDrawingNo);//图纸编号
ui->tableWidget->item(moveRow,orderItemMaterial)->setText(currItemMaterial);//材质
ui->tableWidget->item(moveRow,orderItemQuantity)->setText(currItemQuantity);//数量
ui->tableWidget->item(moveRow,orderItemWorkTime)->setText(currItemWorkTime);//工作时间
((QDateEdit *)ui->tableWidget->cellWidget(moveRow,orderItemDeliveryDate))->setDate(currItemDeliveryDate);//开始时间
ui->tableWidget->item(moveRow,orderItemRemarks)->setText(currItemRemarks);//备注
//列表模型和表格模型的所有数据项都是以根项为父项的,用QModelIndex()表示父项
QModelIndex index = ui->tableWidget->model()->index(moveRow, orderItemNumber, QModelIndex());
ui->tableWidget->setCurrentIndex(index);
下面这两句主要实现选择行跟随移动的行文章来源地址https://www.toymoban.com/news/detail-503904.html
QModelIndex index = ui->tableWidget->model()->index(moveRow, orderItemNumber, QModelIndex());
ui->tableWidget->setCurrentIndex(index);
到了这里,关于QT 实现tablewidget整行的上下移动和双击编辑的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!