我的节点上加入了三个控件,上移,下移和删除
//信号链接部分
connect(ui->btnUp,&QPushButton::clicked,this,&PollPlanTabOperator::onBtnMoveUpClicked);
connect(ui->btnDown,&QPushButton::clicked,this,&PollPlanTabOperator::onBtnMoveDownClicked);
connect(ui->btnDelete,&QPushButton::clicked,this,&PollPlanTabOperator::onBtnDeleteClicked);
//接受信号部分
void PollPlanTabOperator::onBtnMoveUpClicked()
{
QModelIndex indexTemp = m_pollTaskTab->indexAt(QPoint(this->frameGeometry().x(),this->frameGeometry().y()));
upAdownMoveRow(m_pollTaskTab,indexTemp.row(),indexTemp.row() - COUNT_ONE);
//设置Up or Down按钮状态
emit sigSetUpAndDownState();
}
void PollPlanTabOperator::onBtnMoveDownClicked()
{
QModelIndex indexTemp = m_pollTaskTab->indexAt(QPoint(this->frameGeometry().x(),this->frameGeometry().y()));
upAdownMoveRow(m_pollTaskTab,indexTemp.row(),indexTemp.row() + COUNT_TWO);
//设置Up or Down按钮状态
emit sigSetUpAndDownState();
}
void PollPlanTabOperator::onBtnDeleteClicked()
{
QModelIndex indexTemp = m_pollTaskTab->indexAt(QPoint(this->frameGeometry().x(),this->frameGeometry().y()));
m_pollTaskTab->removeRow(indexTemp.row());
//重新排序
updataSequence();
//设置Up or Down按钮状态
emit sigSetUpAndDownState();
}
//最主要的实现部分
void PollPlanTabOperator::upAdownMoveRow(QTableWidget *pTable,int currentRow,int toRow)
{
if (pTable){
int nRowCount = pTable->rowCount();
int nColumnCount = pTable->columnCount();
pTable->setFocus();
if (currentRow != toRow){
if (currentRow < COUNT_ZERO || toRow < COUNT_ZERO || currentRow > nRowCount || toRow > nRowCount)
return;
if (toRow < currentRow)
currentRow ++;
pTable->insertRow(toRow);
for (int count = COUNT_ZERO;count < nColumnCount;count ++){
if (count == TABLE_ROW_THREE){
PollPlanTabStopTimeLine *stopTimeLine = qobject_cast<PollPlanTabStopTimeLine *>(m_pollTaskTab->cellWidget(currentRow,count));
m_pollTaskTab->setCellWidget(toRow,count,stopTimeLine);
}else if (count == TABLE_ROW_FOUR){
PollPlanTabOperator *tabOper = qobject_cast<PollPlanTabOperator *>(m_pollTaskTab->cellWidget(currentRow,count));
m_pollTaskTab->setCellWidget(toRow,count,tabOper);
}else{
pTable->setItem(toRow,count,pTable->takeItem(currentRow,count));
}
}
if (currentRow < toRow)
toRow --;
pTable->removeRow(currentRow);
pTable->selectRow(toRow);
}
}
//updataSequence();
}