Bong.Da.City.
You have since found a convoluted method to achieve the goal. I think it would be far more useful to you in the long run to work out why the forum crowd think it is more convoluted and/or not quite what you wanted. I appreciate that you are a newbie so I will take some time to walk through this.
Back here tbscope gave the conceptual picture of what was needed. That picture contains strong hints as to the exact code required to achieve your aim. You had a go at it but missed a crucial point that you identified yourself a few posts later. As others have pointed out, there is significance to the return value of takeItem() and it is central to the solution the forum has in mind. You are on the right track here.
In an homage to tbscope's ASCII art abilities I have prepared a few of my own. In these diagrams "->" means "points at".
At start with four items in the list:
| |
| |
| |
| |
+---------------------+
listWidget -> +-- QListWidget ------+
| |
| 0 QListWidgetItem * | -> QListWidgetItem A
| |
| 1 QListWidgetItem * | -> QListWidgetItem B
| |
| 2 QListWidgetItem * | -> QListWidgetItem C
| |
| 3 QListWidgetItem * | -> QListWidgetItem D
+---------------------+
To copy to clipboard, switch view to plain text mode
QListWidget::currentRow() returns 2 and corresponds to item C
Remove the item with:
| |
| |
| |
| |
+---------------------+
QListWidgetItem *item = listWidget->takeItem(2);
listWidget -> +-- QListWidget ------+
| |
| 0 QListWidgetItem * | -> QListWidgetItem A
| |
| 1 QListWidgetItem * | -> QListWidgetItem B
| |
| | QListWidgetItem C <- item
| |
| 2 QListWidgetItem * | -> QListWidgetItem D
+---------------------+
To copy to clipboard, switch view to plain text mode
Item now contains a pointer to the removed item. The item still exists in memory but does not exist in the list. You still have access to the removed item through this pointer.
QListWidget::currentRow() returns 2 (still) but points at a different item D. This is why people were telling you that currentRow() was not what you thought it was in your attempt.
Put the item back into the list with:
listWidget->insertItem(1, item);
| |
| |
| |
| |
+---------------------+
listWidget->insertItem(1, item);
listWidget -> +-- QListWidget ------+
| |
| 0 QListWidgetItem * | -> QListWidgetItem A
| |
| 1 QListWidgetItem * | -> QListWidgetItem C
| |
| 2 QListWidgetItem * | -> QListWidgetItem B
| |
| 3 QListWidgetItem * | -> QListWidgetItem D
+---------------------+
To copy to clipboard, switch view to plain text mode
There are still open issues with the approach above. What happens if there is only one item in the list, or you try to move the first/last item up/down? How do you work out where to insertItem()? Where do you want currentRow() to be after the move?
Programming tools like Qt give you a palette of building blocks that can be assembled into a multitude of shapes, just like a bucket of Lego blocks. These blocks as well designed to give you a lot of flexibility without forcing you to reinvent everything from scratch every time. You still need to provide the big picture of what you want to build and work out how the little blocks go together to make the bigger pieces. Much of this comes with practice, as does designing your own intermediate assemblies in a way that can be reused (this orderable list widget combination could be something you reuse).
Bookmarks