Results 1 to 8 of 8

Thread: Move items up and down in QListWidget

  1. #1
    Join Date
    Dec 2008
    Posts
    32
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Move items up and down in QListWidget

    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!

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move items up and down in QListWidget

    The following 2 functions can be used to remove items from and insert items into QListWidget.

    Qt Code:
    1. QListWidgetItem * takeItem ( int row )
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void insertItem ( int row, QListWidgetItem * item )
    To copy to clipboard, switch view to plain text mode 

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

  3. The following user says thank you to munna for this useful post:

    araglin (13th January 2009)

  4. #3
    Join Date
    Dec 2008
    Posts
    32
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move items up and down in QListWidget

    Thank you for rapid answer. I understood how to use this functions, now will try to do it.
    :-)

  5. #4
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Move items up and down in QListWidget

    This is how I did mine. It works but I don't know if its the right way

    Qt Code:
    1. void
    2. ServerPage::moveServerDown() {
    3. QListWidgetItem *current = serverList->currentItem();
    4. int currIndex = serverList->row(current);
    5.  
    6. QListWidgetItem *next = serverList->item(serverList->row(current) + 1);
    7. int nextIndex = serverList->row(next);
    8.  
    9. QListWidgetItem *temp = serverList->takeItem(nextIndex);
    10. serverList->insertItem(currIndex, temp);
    11. serverList->insertItem(nextIndex, current);
    12. }
    13.  
    14. void
    15. ServerPage::moveServerUp() {
    16. QListWidgetItem *current = serverList->currentItem();
    17. int currIndex = serverList->row(current);
    18.  
    19. QListWidgetItem *prev = serverList->item(serverList->row(current) - 1);
    20. int prevIndex = serverList->row(prev);
    21.  
    22. QListWidgetItem *temp = serverList->takeItem(prevIndex);
    23. serverList->insertItem(prevIndex, current);
    24. serverList->insertItem(currIndex, temp);
    25. }
    To copy to clipboard, switch view to plain text mode 
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  6. The following user says thank you to ComaWhite for this useful post:

    Lpcnew (1st June 2010)

  7. #5
    Join Date
    Jan 2010
    Posts
    1
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Move items up and down in QListWidget

    comaWhite, thaks a lot.... it was very useful!! =)

  8. #6
    Join Date
    Dec 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb Re: Move items up and down in QListWidget

    It is the right way but you can remove some lines.
    I added moveTop and moveBottom:
    Qt Code:
    1. void moveDown(void )
    2. {
    3. int currentIndex = list->currentRow();
    4. QListWidgetItem *currentItem = list->takeItem(currentIndex);
    5. list->insertItem(currentIndex+1, currentItem);
    6. list->setCurrentRow(currentIndex+1);
    7. }
    8.  
    9.  
    10. void moveTop(void )
    11. {
    12. int currentIndex = list->currentRow();
    13. QListWidgetItem *currentItem = list->takeItem(currentIndex);
    14. list->insertItem(0, currentItem);
    15. list->setCurrentRow(0);
    16. }
    17.  
    18.  
    19. void moveBottom(void )
    20. {
    21. int currentIndex = list->currentRow();
    22. QListWidgetItem *currentItem = list->takeItem(currentIndex);
    23. list->insertItem(list->count(), currentItem);
    24. list->setCurrentRow(list->count()-1);
    25. }
    26.  
    27.  
    28. void moveUp(void )
    29. {
    30. int currentIndex = list->currentRow();
    31. QListWidgetItem *currentItem = list->takeItem(currentIndex);
    32. list->insertItem(currentIndex-1, currentItem);
    33. list->setCurrentRow(currentIndex-1);
    34. }
    To copy to clipboard, switch view to plain text mode 

  9. #7
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    4

    Default Re: Move items up and down in QListWidget

    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.

    Qt Code:
    1. void ListItemSwap(QListWidget *list,QString direction)
    2. {
    3. int currentIndex = list->currentRow();
    4. if (currentIndex!=-1)
    5. {
    6. if (direction.toLower()=="up") //moves the item up
    7. {
    8. if (currentIndex>0)
    9. {
    10. QListWidgetItem *currentItem = list->takeItem(currentIndex);
    11. list->insertItem(currentIndex-1, currentItem);
    12. list->setCurrentRow(currentIndex-1);
    13. }
    14. }
    15. else //moves the item down
    16. {
    17. if (currentIndex<list->count()-1)
    18. {
    19. QListWidgetItem *currentItem = list->takeItem(currentIndex);
    20. list->insertItem(currentIndex+1, currentItem);
    21. list->setCurrentRow(currentIndex+1);
    22. }
    23. }
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    and I call it like this:
    Qt Code:
    1. ListItemSwap(ui->myListViewWidget,"up")
    2. ...
    3. ListItemSwap(ui->myListViewWidget,"down")
    To copy to clipboard, switch view to plain text mode 

  10. #8
    Join Date
    Nov 2014
    Posts
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Move items up and down in QListWidget

    Thank you "Spomky".

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.