PDA

View Full Version : Accessing and changing the order/index of QListWidget items



Kyle_Katarn
12th April 2011, 18:47
Hey, still working on the batch renamer, but I'm making it a little more robust with the addition of the Name Convention dialog.

http://img27.imageshack.us/img27/4417/brn03.png

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

stampede
12th April 2011, 20:14
I cant find anything that gets the current selected item
You can use QListWidget::currentRow (http://doc.qt.nokia.com/latest/qlistwidget.html#currentRow-prop). 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 (http://doc.qt.nokia.com/latest/qlistwidget.html#takeItem)) and insert again reordered (QList::insertItem (http://doc.qt.nokia.com/latest/qlistwidget.html#insertItem)). This works, I have a working code, but I won't post it, because it's a newbie forum :p

Kyle_Katarn
12th April 2011, 20:45
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. :)

Kyle_Katarn
13th April 2011, 06:43
Well, it did work, but after fiddling around with some voodoo. The current code I have is:


void NamingConvention::moveCurrentItemDown()
{
int currentRow = this->lstOrder->currentRow();
int nextRow = currentRow + 1;
QListWidgetItem* itemOne = this->lstOrder->item( currentRow );
QListWidgetItem* itemTwo = this->lstOrder->item( nextRow );

this->lstOrder->takeItem( currentRow + 1);
this->lstOrder->takeItem( nextRow + 2 );

this->lstOrder->insertItem( currentRow, itemTwo );
this->lstOrder->insertItem( nextRow, itemOne );

updateLabel();
}

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. :)