思路:
1.首先数据来源需要格式都一样的,比如这样:
2.然后进行每一列进行文字比对一样的就合并。
void mSpanItem(QTableWidget* pTable)
{
if (pTable == NULL)
return;
int row = pTable->rowCount();
for (int columnIndex = 0; columnIndex < 2; columnIndex++)
{
int j = 0;
for (int i = 0; i < row - 1; i++)
{
QTableWidgetItem* pItem = pTable->item(i, columnIndex);
QString text;
if (NULL != pItem)
{
text = pItem->text();
}
if (text.isEmpty())
{
return;
}
else
{
QString NextText;
pItem = pTable->item(i + 1, columnIndex);
if (NULL != pItem)
{
NextText = pItem->text();
}
if (text != NextText)
{
pTable->setSpan(j, columnIndex, i - j + 1, 1);
if (columnIndex == 1)
{
pTable->setSpan(j, 2, i - j + 1, 1);
}
j = i + 1;
}
}
}
pTable->setSpan(j, columnIndex, row - j, 1);
if (columnIndex == 1)
{
pTable->setSpan(j, 2, row - j, 1);
}
}
}
结果: