Results 1 to 4 of 4

Thread: Accessing and changing the order/index of QListWidget items

  1. #1
    Join Date
    Apr 2011
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Accessing and changing the order/index of QListWidget items

    Hey, still working on the batch renamer, but I'm making it a little more robust with the addition of the Name Convention dialog.



    As you can see, the layout is not much different to the "File Name..." dialog found in Windows Media Player's options menu, and I plan for similar functionality as well, that is if you have an item selected in the List View, and press the Move Down button, the item's order will move down, same thing for the move up button (I've already implemented a feature that will disable the Move Up or Down buttons based on whether you have the first or last item selected), but as for the actual reordering of said items has me in a spin, I cant find anything that gets the current selected item, nor can I find anything that will let me change its index.

    Any help would be greatly appreciated.

    Thanks.
    ~Kyle

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Accessing and changing the order/index of QListWidget items

    I cant find anything that gets the current selected item
    You can use QListWidget::currentRow. Each "up" or "down" operation requires to change the index of two items in list, so you can just remove them from list (QListWidget::takeItem) and insert again reordered (QList::insertItem). This works, I have a working code, but I won't post it, because it's a newbie forum

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

    Kyle_Katarn (12th April 2011)

  4. #3
    Join Date
    Apr 2011
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Accessing and changing the order/index of QListWidget items

    Haha, I probably should have stated in the original post, but no I wasn't after any code at all, all I needed was a nudge in the right direction and you more than gave that to me. Thanks a bunch, I will try it out after I've had some sleep and post how I went.

    Thanks again.

  5. #4
    Join Date
    Apr 2011
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Accessing and changing the order/index of QListWidget items

    Well, it did work, but after fiddling around with some voodoo. The current code I have is:

    Qt Code:
    1. void NamingConvention::moveCurrentItemDown()
    2. {
    3. int currentRow = this->lstOrder->currentRow();
    4. int nextRow = currentRow + 1;
    5. QListWidgetItem* itemOne = this->lstOrder->item( currentRow );
    6. QListWidgetItem* itemTwo = this->lstOrder->item( nextRow );
    7.  
    8. this->lstOrder->takeItem( currentRow + 1);
    9. this->lstOrder->takeItem( nextRow + 2 );
    10.  
    11. this->lstOrder->insertItem( currentRow, itemTwo );
    12. this->lstOrder->insertItem( nextRow, itemOne );
    13.  
    14. updateLabel();
    15. }
    To copy to clipboard, switch view to plain text mode 

    I had trouble with the takeItem part, and the only way I could make it work was to add the +1 and +2 to the rows in question for it to work properly, otherwise it would make one or two of the items disappear. I cant understand why though, as I understand takeItem returns a 0 if it was unsuccessful (something I personally dont agree with, returning -1 would be a better option in my opinion), so you'd need to pass in a 1 to literally get the first row, which I understand alright, but then I need to take the next item and it wont work unless I have the +2 in there, if I have what would logically be there (+1) it would just make the selected item disappear. I would greatly appreciate someone explaining why this is to my rather slow mind.

    Thanks again though stampede, what I do have does work.

Similar Threads

  1. Replies: 2
    Last Post: 28th October 2016, 11:12
  2. Replies: 2
    Last Post: 1st April 2011, 09:32
  3. Replies: 5
    Last Post: 25th February 2010, 17:29
  4. Changing column order in QTableView
    By Banjo in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2008, 06:49
  5. Changing the order of columns in QTreeView
    By johnny_sparx in forum Qt Programming
    Replies: 1
    Last Post: 15th February 2006, 00:00

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.