PDA

View Full Version : Move items up and down in QListWidget



araglin
13th January 2009, 04:16
Hello!
I have a QListWidget and two buttons ("move item up" and "move item down").
So, the list represents something like stack and it would be nice to give the ability to move selected item up/down in this stack to users.
Would you please suggest the right way to add this functionality?
Thanks!

munna
13th January 2009, 04:21
The following 2 functions can be used to remove items from and insert items into QListWidget.


QListWidgetItem * takeItem ( int row )

void insertItem ( int row, QListWidgetItem * item )

Using these 2 you should be able to move items up and down your stack.

araglin
13th January 2009, 06:11
Thank you for rapid answer. I understood how to use this functions, now will try to do it.
:-)

ComaWhite
24th January 2009, 05:23
This is how I did mine. It works but I don't know if its the right way



void
ServerPage::moveServerDown() {
QListWidgetItem *current = serverList->currentItem();
int currIndex = serverList->row(current);

QListWidgetItem *next = serverList->item(serverList->row(current) + 1);
int nextIndex = serverList->row(next);

QListWidgetItem *temp = serverList->takeItem(nextIndex);
serverList->insertItem(currIndex, temp);
serverList->insertItem(nextIndex, current);
}

void
ServerPage::moveServerUp() {
QListWidgetItem *current = serverList->currentItem();
int currIndex = serverList->row(current);

QListWidgetItem *prev = serverList->item(serverList->row(current) - 1);
int prevIndex = serverList->row(prev);

QListWidgetItem *temp = serverList->takeItem(prevIndex);
serverList->insertItem(prevIndex, current);
serverList->insertItem(currIndex, temp);
}

Lpcnew
1st June 2010, 20:50
comaWhite, thaks a lot.... it was very useful!! =):)

Spomky
10th December 2011, 15:37
It is the right way but you can remove some lines.
I added moveTop and moveBottom:

void moveDown(void )
{
int currentIndex = list->currentRow();
QListWidgetItem *currentItem = list->takeItem(currentIndex);
list->insertItem(currentIndex+1, currentItem);
list->setCurrentRow(currentIndex+1);
}


void moveTop(void )
{
int currentIndex = list->currentRow();
QListWidgetItem *currentItem = list->takeItem(currentIndex);
list->insertItem(0, currentItem);
list->setCurrentRow(0);
}


void moveBottom(void )
{
int currentIndex = list->currentRow();
QListWidgetItem *currentItem = list->takeItem(currentIndex);
list->insertItem(list->count(), currentItem);
list->setCurrentRow(list->count()-1);
}


void moveUp(void )
{
int currentIndex = list->currentRow();
QListWidgetItem *currentItem = list->takeItem(currentIndex);
list->insertItem(currentIndex-1, currentItem);
list->setCurrentRow(currentIndex-1);
}

dv8
23rd January 2013, 07:32
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.



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);
}
}
}
}


and I call it like this:


ListItemSwap(ui->myListViewWidget,"up")
...
ListItemSwap(ui->myListViewWidget,"down")

amol.palshetkar
31st August 2016, 11:05
Thank you "Spomky".