示例图如下:
//删除第三列值的行处理函数
void delRow(QTableWidgetItem* item)
{
int row = item->row();
int column = item->column();
QString deleteRowOneColumnText = m_table->item(row, 1)->text();
QTableWidgetItem* frontItem = NULL;
QTableWidgetItem* nextItem = NULL;
if (row > 0)
{
frontItem = m_table->item(item->row() - 1, item->column());
}
if (row < m_table->rowCount())
{
nextItem = m_table->item(row + 1, item->column());
}
if (frontItem == NULL && nextItem != NULL)
{
if (deleteRowOneColumnText.compare(m_table->item(nextItem->row(), 0)->text()) != 0)
{
m_table->item(row, 2)->setText("");
}
else
{
m_table->removeRow(row);
}
}
else if (frontItem != NULL && nextItem == NULL)
{
if (deleteRowOneColumnText.compare(m_table->item(frontItem->row(), 0)->text()) != 0)
{
m_table->item(row, 2)->setText("");
}
else
{
m_table->removeRow(row);
}
}
else if (frontItem != NULL && nextItem != NULL)
{
QString frontText = m_table->item(frontItem->row(), 1)->text();
QString nextText = m_table->item(nextItem->row(), 1)->text();
if (deleteRowOneColumnText.compare(frontText) == 0 || deleteRowOneColumnText.compare(nextText) == 0)
{
m_table->removeRow(row);
}
else if (deleteRowOneColumnText.compare(frontText) != 0 && deleteRowOneColumnText.compare(nextText) != 0)
{
m_table->item(row, 2)->setText("");
}
}
}