Results 1 to 8 of 8

Thread: Move items up and down in QListWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

  2. #2
    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 

  3. #3
    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
  •  
Qt is a trademark of The Qt Company.