QListWidget的移动
记录对QListWidgetItem在QListWidget中的上下移动
/**
* 这是简单的对QListWidgetItem的上下移动
* bool bUp 是用来标识上移或者下移
* true 上移
* false 下移
* 已知的变量
* QListWidget* listWidget
*/
void ItemMoveInListWidget(bool bUp = /* false */)
{
// 获得当前选中或者需要移动的item所在的行
int currentRow = listWidget->currentRow();
if (currentRow == -1)
return;
// 计算上移或者下移之后要到达的位置
int arriveRow = bUp ? (currentRow - 1 < 0 ? 0 : currentRow - 1) : (currentRow + 1 >= listWidget->count() ? listWidget>count() - 1 : currentRow + 1);
// 删除行数大的Item,然后插入到行数小的Item前
listWidget->insertItem(min(currentRow, arriveRow), listWidget->takeItem(max(currentRow, arriveRow)));
// 然后选中到达的行
listWidget->setCurrentRow(arriveRow);
return;
}