Hi,
this thread was very helpful to me, so I wanted to add my 2 cents... 
I've combined the "moveDown" and "moveUp" functions into one "ListItemSwap" function.
I've also added some checks to ensure the currently selected item always stays selected even if you try to move it up over the top of the list or down under the bottom of the list.
I hope it will be useful to someone else too.
{
int currentIndex = list->currentRow();
if (currentIndex!=-1)
{
if (direction.toLower()=="up") //moves the item up
{
if (currentIndex>0)
{
list->insertItem(currentIndex-1, currentItem);
list->setCurrentRow(currentIndex-1);
}
}
else //moves the item down
{
if (currentIndex<list->count()-1)
{
list->insertItem(currentIndex+1, currentItem);
list->setCurrentRow(currentIndex+1);
}
}
}
}
void ListItemSwap(QListWidget *list,QString direction)
{
int currentIndex = list->currentRow();
if (currentIndex!=-1)
{
if (direction.toLower()=="up") //moves the item up
{
if (currentIndex>0)
{
QListWidgetItem *currentItem = list->takeItem(currentIndex);
list->insertItem(currentIndex-1, currentItem);
list->setCurrentRow(currentIndex-1);
}
}
else //moves the item down
{
if (currentIndex<list->count()-1)
{
QListWidgetItem *currentItem = list->takeItem(currentIndex);
list->insertItem(currentIndex+1, currentItem);
list->setCurrentRow(currentIndex+1);
}
}
}
}
To copy to clipboard, switch view to plain text mode
and I call it like this:
ListItemSwap(ui->myListViewWidget,"up")
...
ListItemSwap(ui->myListViewWidget,"down")
ListItemSwap(ui->myListViewWidget,"up")
...
ListItemSwap(ui->myListViewWidget,"down")
To copy to clipboard, switch view to plain text mode
Bookmarks